excel - 在 VBA excel 中的 VLOOKUP 中设置 table_array

标签 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 Range("BG2:BG" & Final_row)
        .FormulaR1C1 = "=vlookup(RC[-52],lookup_range, 2, False)"

    End With

End Sub

最佳答案

尝试,

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

Range.Address property可以返回整个路径和工作表名称。付出太多并没有坏处。

这可能是一个更好的方法。
    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

关于excel - 在 VBA excel 中的 VLOOKUP 中设置 table_array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35474314/

相关文章:

sql-server - 无法将存储过程结果返回到 Excel

excel - 将 ColB 和 ColC 中的数据移动到 ColA 中的数据下方

excel - 如何从 .cab 文件安装 mscomct2.ocx 文件(Excel 用户表单和 VBA)

vba - Excel VBA 连接到 IBM DB2

excel - 两个 Excel 宏似乎相互冲突并阻止彼此工作

vba - 为什么当long变量在允许范围内时会弹出 "Run-time error ' 6': Overflow"?

vba - 将嵌入图像转换为链接

function - Excel IF 语句中的范围

excel - 如何添加工作表 1 中的值并在工作表 2 上显示总金额?

c# - 使用图表在 Excel 中创建辅助轴