尝试使用Range.Formula属性时格式错误的VLookup公式

时间:2012-07-05 21:14:10

标签: vba excel-vba vlookup excel

我有Excel表格,其中包含来自许多来源的数据,这些数据将被组合在一起,以便需要查看的内容高于您所看到的内容,因此在一张纸的不同部分上会有许多VLookup。

Sub linkFDCfdv()
Range("A1").Select
Dim doesFDChaveDescription As Boolean
Dim isLastRowFDC As Boolean
Dim myRange As String
Dim firstFDCrow As Long
Dim lastFDCrow As Long
While Len(Selection.Value) > 0

    If Selection.Value = "FDC" Then

        If isLastRowFDC = False Then
            firstFDCrow = ActiveCell.Row

        End If

        isLastRowFDC = True

        ActiveCell.Offset(0, 3).range("A1").Select

        If Len(Selection.Value) > 0 Then
            doesFDChaveDescription = True
        Else
            doesFDChaveDescription = False
        End If
        ActiveCell.Offset(0, -3).range("A1").Select
    Else
        If isLastRowFDC = True Then
            lastFDCrow = ActiveCell.Row - 1
        End If

    End If

    If Selection.Value = "FDV" Then

        ActiveCell.Offset(0, 10).range("A1").Select

        myRange = "B" & firstFDCrow & ":D" & lastFDCrow

        ActiveCell.Formula = "=VLOOKUP(R[0]C[-2]," & myRange & ",2)"


        ActiveCell.Offset(0, -10).range("A1").Select
    End If


    ActiveCell.Offset(1, 0).range("A1").Select

Wend

End Sub

我的宏发生了什么:

=VLOOKUP(I9,'B3':'D8',2)

如果我取出'标记,则宏可以正常工作。

1 个答案:

答案 0 :(得分:3)

那是因为你正在使用R1C1风格和A1风格的混合。这是你在尝试什么?

ActiveCell.Formula = "=VLOOKUP(" & ActiveCell.Offset(,-2).Address & _
                     "," & myRange & ",2)"