I’m having an issue with a variable worksheet within a vlookup formula. I believe it is something to do with how I’m referenceing the worksheet. I have looked a numerious other blogs on this and used different techniques but I’m still getting a “Run-time Error 1004”. Below is a summary of the related code – any help would be greatly appreciated.
Sub PopulateDynamicReport()
Dim getcolumnPDR As Range
Dim getConfigPosition As Range
Dim getFieldDescriptionPDR As String
Dim getFormulaPDR As Variant
Dim columnletter As String
Dim myrange As String
Dim getColumnLetter As String
Dim counter As Long
Dim lLastrow As Long
Dim ws As Worksheet
lLastrow = FindLastRow("Pricing Analysis")
Sheets("Pricing Analysis").Cells.Clear
counter = 1
Set getConfigPosition = Sheets("Config").Cells.Find(What:="Field Description", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Offset(counter, 0)
columnletter = getConfigPosition.Offset(0, -1)
Set ws = Sheets(getConfigPosition.Offset(0, 2).Value)
Sheets("Pricing Analysis").Cells(1, columnletter).FormulaR1C1 = "=VLOOKUP(RC[-4],ws.range(!C[-4]:C[-2]),3,FALSE)"
counter = counter + 1
End Sub
Try this:
You can not mix the vba objects into formulas that you write on a sheet, but you can use the vba objects to help build the string you need to write the formula, as I have done above.