Vlookupvariables参考问题

我在一个vlookup公式中的variables工作表有问题。 我相信这是我如何引用工作表。 我已经看了这个数字的其他博客,并使用不同的技术,但我仍然得到“运行时错误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 

尝试这个:

 "=VLOOKUP(RC[-4]," & ws.Name & "!RC[-4]:RC[-2],3,FALSE)" 

您不能将vba对象混合到您在工作表上编写的公式中,但可以使用vba对象来帮助构build编写公式所需的string,如上所述。