1

我在 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
4

1 回答 1

2

试试这个:

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

您不能将 vba 对象混合到您在工作表上编写的公式中,但您可以使用 vba 对象来帮助构建编写公式所需的字符串,就像我在上面所做的那样。

于 2013-01-14T14:56:03.923 回答