我在vlookup公式中遇到了变量工作表的问题。我相信这与我如何引用工作表有关。我已经看了很多其他博客,并使用了不同的技术,但我仍然得到一个“运行时错误1004”。以下是相关代码的摘要 - 非常感谢任何帮助。
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
答案 0 :(得分:2)
试试这个:
"=VLOOKUP(RC[-4]," & ws.Name & "!RC[-4]:RC[-2],3,FALSE)"
您不能将vba对象混合到您在工作表上编写的公式中,但您可以使用vba对象来帮助构建编写公式所需的字符串,如上所述。