Excel VBA formula R1C1 Vlookup - vba

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)"

Related

Excel formula within VBA

I have quite a few cells that contain formula, then with VBA the outcome of this formula is the value for a variable, like so:
On sheet in cell AS4:
=SUMPRODUCT(MAX((ROW($AE$4:$AE$997))*($AE$4:$AE$997<>"")))
and then in my VBA:
numRows = ws.Range("AS4").Value
However this is starting to get hard to keep track of which cell is feeding which variable, avoiding overwriting those cells on the sheet by accident, etc.
I need to be able to perform this calculation within VBA if I can, removing the need to have "calculation cells" on my sheet.
I have discovered there is a way to use formula with WorksheetFunction, but only found simple examples of this and cannot adapt it to my situation above.
numRows = WorksheetFunction.SumProduct(MAX((ROW($AE$4:$AE$997))*($AE$4:$AE$997<>"")))
Is not going to work...
Is there a way to do this, or am I better scrapping the idea of using formula and using a pure VBA method?
With help from SJR this was the answer:
numRows = [=SUMPRODUCT(MAX((ROW(Weights!$AE$4:$AE$997))*(Weights!$AE$4:$AE$997<>"""")))]
A bit more research taught me that evaluate(" ") can be just replaced with square brackets [ and ]. Although, if I had variables in the mix of this formula or the formula wasn't constant then I would have to use Evaluate.
I also needed to add the sheet name to the formula as this formula was no longer functioning within the sheet and AE4:AE997 was no longer referring to the correct sheet.
Doubling up on quotes is also necessary as it is code and sees " differently to a formula on the sheet

Transfer the combination formula into 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

How to split a cell's formula based on functions using VBA?

I have a working formula in a cell:
SUMIF(I_Income!$B:$B,'C_I&E (real)'!$B18,I_Income!T:T)+'C_SDev (real)'!T18+'C_Efficiency (real)'!T18
and I'd like to know if it is possible to extract the SUMIF portion of this formula using VBA? So ignore the other gumph and just end up with a variable in VBA matching SUMIF(I_Income!$B:$B,'C_I&E (real)'!$B18,I_Income!T:T).
So in summary, how do I ignore everything in the formula except for the SUMIF portion?

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)"