Transfer the combination formula into VBA - vba

I have a formula in excel, that is combination between IF statement, vlookup and they formula will be application in all cell using looping coding.
this the formula :
= IF((VLOOKUP(G4;$A$4:$B$9;2;FALSE))=1;"terhubung";IF((VLOOKUP(G4;$A$4:$C$9;3;FALSE))=1;"unreach";IF((VLOOKUP(G4;$A$4:$D$9;4;FALSE))=1;"reject";IF((VLOOKUP(G4;$A$4:$E$9;5;FALSE))=1;"workload";""))))
explanation about the formula, I try capture in this image.
The question is how to the formula can transfer into the vba coding using button_click
Thanks

You can replace your formula with the following one...
=INDEX($B$3:$E$3,MATCH(1,INDEX($B$4:$E$9,MATCH(G4,$A$4:$A$9,0),),0))
If you want to insert the formula through VBA and keep the values only, you may try something like below.
Sub InsertFormula()
Range("H4:H9").Formula = "=INDEX($B$3:$E$3,MATCH(1,INDEX($B$4:$E$9,MATCH(G4,$A$4:$A$9,0),),0))"
Range("H4:H9").Value = Range("H4:H9").Value
End Sub

Related

Excel VBA formula R1C1 Vlookup

I am using the vlookup function in excel. by right formula is this:
"=VLOOKUP([Engagement Id],Pivots!AL:AU,10,FALSE)"
When i do my vba, it changes to
Range("CT2").FormulaR1C1 = _
"=VLOOKUP([Engagement Id],Pivots!C[-60]:C[-51],10,FALSE)"
I dont want this -60:-51 and I want the letters itself. but once i change the formula to have AL: AU instead of -60 :-51 it doesnt work. Anyone knows what to do?
Adrian
R1C1 formulas use indexed cell coordinates (numbers), if you want to use letters don't use R1C1 formula but classic ones:
Range("CT2").Formula = "=VLOOKUP([Engagement Id],Pivots!AL:AU,10,FALSE)"

Calculating Formula After Text Paste

I have a formula that constructs a vlookup function while drawing the file path from a date input from the user, next I have a macro that takes the results of this formula and pastes them in Cell C6.
=("=" & "IF(ISNA(VLOOKUP($A6," & "'I:\CM\PC\A\TR\AT\"&TEXT(C$5,"yyyy")&"\"&TEXT(C$5,"mmmmmmmmmmm")&"\["&TEXT(C$5,"mmm")&TEXT(C$5,"d")&".xlsm]Violations Count'!$A:$B"&",2,FALSE)),"""",VLOOKUP($A6,"&"'I:\CM\PC\A\TR\AT\"&TEXT(C$5,"yyyy")&"\"&TEXT(C$5,"mmmmmmmmmmm")&"\["&TEXT(C$5,"mmm")&TEXT(C$5,"d")&".xlsm]Violations Count'!$A:$B"&",2,FALSE))")
The trouble I am having is finding a way for VBA to activate the formula instead of having it sit there in a text format as:
=IF(ISNA(VLOOKUP($A6,'I:\CM\PC\A\TR\AT\2016\December\[Dec5.xlsm]Violations Count'!$A:$B,2,FALSE)),"",VLOOKUP($A6,'I:\CM\PC\A\TR\AT\2016\December\[Dec5.xlsm]Violations Count'!$A:$B,2,FALSE))
Afterward I just have a macro autofill it down for the other names between column A6:A37.
If anyone can help me with VBA activating the cell, it would be much appreciated.
If you want to get the value of a cell (which might contain a text string) you use something like:
someVariableEtc = Range("A1").Value
If you want to set a Cell's formula using VBA, you would use something like:
Range("C6").Formula = "some text string"
Combining the two, you could come up with something like:
Range("C6").Formula = Range("A1").Value
Look this up. The evaluate function might be what you're looking for. The simplest way to use it is wrapping an expression in square brackets [].

How to get commented cells with SQL QUERY?

in Excel, there's an option to write comments on your cells.
I want to get the content of these commented cells (with a SELECT SQL query), but i don't know if this is possible and what should i put in my "WHERE" condition.
Can you help me ?
Thank you
Excel is not a database as such and cannot SQL queries on excel objects(Worsheet, Cell, Range....).
It has its own DOM (Document Object Model) which can be used to access the objects and their properties and methods. Excel VBA programming will help us here to access these objects and their details.
A simple Excel VBA code to access the comment text is given below:
Sub CommandButton1_Click()
Range("A2").Value = Range("A1").Comment.Text
End Sub
Here Cell A1 has a comment with certain text. This comment text value is captured and stored in Cell A2.

How to put a formula into a cell by Excel VBA?

I'm trying to put this =IF(D49>0,D49-D50-D51+D52+D53,) into a cell D54 by using macro.
My code is as follow
Range("D54").FormulaR1C1 = "=IF(D52>0,D49-D50-D51+D52+D53,)"
But when I run the macro.
Cell D54 gets this =IF('D52'>0,'D49'-'D50'-'D51'+'D52'+'D53',) instead.
Excel adds quotation marks to each dells in the formula, render the formula useless. How can I get macro to into formula as formula?
I think you are having issues because you are using the R1C1 formula property for your range, yet you are using classical cell references in your formula. You should be using the Range("D54").Formula property. So your code would look like:
Range("D54").Formula = "=IF(D52>0,D49-D50-D51+D52+D53,)"
You could just use Range("D54").Value = "=IF(D52>0,D49-D50-D51+D52+D53,)"
I have used that method before and hand good results with it.

How to use "This Row" in VBA VLOOKUP?

I am trying to modify a Vlookup function so that it uses "this row" functionality. For example I have a macro that places in Column B the Following formula:
=VLOOKUP(I1253,treatlookup,11,FALSE)
This works well, however during the macro, I need to have it switch over to another formula:
=VLOOKUP(I1253,Itemlookup,22,FALSE)
Of course the data varies everytime so I can not just have that formula put in at a specific cell. So what I would like the formula to state is
= Vlookup(I"CurrentRow", ItemLookup, 22,False)
I can then use an if statement to determine which of the two formulas to use.
thanks in Advance Using Excel 2010.
Use R1C1 style:
Activecell.FormulaR1C1 = "=VLOOKUP(RC9, ItemLookup, 22, false)"