How to use "This Row" in VBA VLOOKUP? - vba

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

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

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 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.

VB code for VLOOKUP

I'm completely new to VBA. I tried recording a macro of using VLOOKUP function in my spreadsheet, but turns out VLOOKUP recorded like that does not work.
I have a working spreadsheet and I am comparing values in the column "Material" with a different workbook that is like a database for all the materials and their respective programs. The program the material number matches to in the database will be recorded under column "Program" in the working spreadsheet. I use VLOOKUP to do this, but I am having a hard time coming up with a macro for it.
As suggested, I did the following:
ActiveCell.FormulaR1C1 = "'Program"
Range("K41").Select
ActiveCell.Formula = "VLOOKUP(D41,"'C:\Users\Username\Desktop[Database.xlsx]Sheet1!R1C1:R152289C4,4,FALSE)"
But it still does not populate the "Program" column. What am I doing wrong?
The exact line for vlookups can be done in two ways;
1) This does a vlookup within the coding environment, it's more difficult to make it dynamic
application.worksheetfunction.vlookup(worksheets("Material").range("A2"), worksheets("Program").range("A1:F10"),4,0)
2) Make the formula in the cell the vlookup
range("c2").select
activecell.formula = "=vlookup(A1,Material!A1:F10,4,0)"
and then use the environment to fill in.
Maudise
Big thank you to all who took the time to address my question. I took suggestions, played around with the formula, and this is what I got:
=VLOOKUP($D41,'C:\Users\Username\Desktop[Database.xlsx]Sheet 1'!$A$1:$B$40973,2,FALSE)
Does exactly what I wanted it to do! Thanks again!