I was hoping to define some variables and put them within a vlookup formula. I have outlined below a simple example of what I’m trying to achieve. However I keep getting a ‘438’ error message.
I have put in the original vlookup without the variables and the vlookup with the variables in that are not working. Any input would be appreciated. I believe I have variables defined correctly but I think it’s something to do with the way I’m calling them in the formula.
Sub Macro1()
Dim Lookupcolumn As Range
Dim columnletter_start As String
Dim columnletter_End As String
columnletter_start = ActiveCell.offfet(0, 1).Value
columnletter_End = ActiveCell.offfet(0, 2).Value
Set Lookupcolumn = Sheets("Pricing Analysis").Cells.Find(What:="Cusip", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
'ActiveCell.FormulaR1C1 = "=VLOOKUP(R[1]C[-8],'CS Primeview'!C[-6]:C[-4],3,FALSE)"
ActiveCell.FormulaR1C1 = "=VLOOKUP(Lookupcolumn,'CS Primeview'columnletter_start:columnletter_End,3,FALSE)"
End Sub
The problem is that indeed you may use any Excel function in your VBA code via
Excel.WorksheetFunction.structure and obtain correct results using variables.However, for your code
ActiveCell.FormulaR1C1 = "=VLOOKUP(Lookupcolumn,'CS Primeview'columnletter_start:columnletter_End,3,FALSE)"you try to use Range object as reference inVLOOKUP, but Excel understands as the argument only valid reference asA1or Named Region.As a solution try to generate
A1-similar argument in your code. For the entire columnA:Awill go. Good luck!P.S. One more thing I noticed:
'CS Primeview'columnletter_start:columnletter_End– for this part it seems you missed exclamation before reference, as well as double quotes to concatenate parts, i.e."'CS Primeview'!" & columnletter_start & ":" & columnletter_Endshould be correct.