在VBA excel中的VLOOKUP中设置表格数组

在VBA excel中的VLOOKUP中设置表格数组,excel,vba,Excel,Vba,我正在VBA中尝试vlookup。我正在引用同一工作簿中的另一张工作表,但结果得到了#NAME? Sub MatchNames() l_row = Worksheets("Lookup").Cells(Rows.Count, 1).End(xlUp).Row lookup_range = Worksheets("Lookup").Range("A2:E" & l_row) Final_row = Cells(Rows.Count, 6).End(xlUp).Row With

我正在VBA中尝试vlookup。我正在引用同一工作簿中的另一张工作表,但结果得到了
#NAME?

Sub MatchNames()

l_row = Worksheets("Lookup").Cells(Rows.Count, 1).End(xlUp).Row
lookup_range = Worksheets("Lookup").Range("A2:E" & l_row)

Final_row = Cells(Rows.Count, 6).End(xlUp).Row

    With Range("BG2:BG" & Final_row)
        .FormulaR1C1 = "=vlookup(RC[-52],lookup_range, 2, False)"

    End With

End Sub
试试看


@Ageonix可能重复-我不认为链接的答案是重复的。在该链接答案中,问题是使用工作表名称作为外部范围;这里使用声明的var作为命名范围。谢谢大家@我用了你的解决方案,效果很好。出于好奇,你知道我的代码有什么问题吗?
l_row = Worksheets("Lookup").Cells(Rows.Count, 1).End(xlUp).Row
Set lookup_range = Worksheets("Lookup").Range("A2:E" & l_row)  '<~~one edit here

With Range("BG2:BG" & Final_row)
    .FormulaR1C1 = "=vlookup(RC[-52]," & lookup_range.address(referencestyle:=xlR1C1, external:=true) & ", 2, False)"  '<~~another edit here
End With
    Dim l_row As Long, f_row As Long, sLookup_Range As String

    With Worksheets("Lookup")
        l_row = .Cells(Rows.Count, "A").End(xlUp).Row
        sLookup_Range = .Range("A2:E" & l_row).Address(ReferenceStyle:=xlR1C1, external:=True)
    End With

    With Worksheets("Sheet1")
        f_row = .Cells(Rows.Count, "G").End(xlUp).Row
        With .Range("BG2:BG" & f_row)
            .FormulaR1C1 = "=vlookup(RC[-52]," & sLookup_Range & ", 2, False)"
        End With
    End With