Excel VBA在vlookup公式中使用单元格值

我正在尝试创build一个名称与活动工作表中单元格值相同的工作表的vlookup。

我创build了以下想法,我可以在我的vlookup公式中使用'str',但是,我得到'运行时错误'1004':应用程序定义或对象定义的错误'

Sub copy() Dim LastRow As Long Dim str As String str = Cells(1, 5).Value With Sheets("Overview") LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row With .Range("E2:E" & LastRow) .Formula = "=VLOOKUP(B2, & str &!B:F,5,FALSE)" End With End With End Sub 

任何人都可以看到我做错了什么?

您已经在VBA中定义了str ,但在公式中引用了它,而不用closures引号,请尝试:

 Sub copy() Dim LastRow As Long Dim str As String str = Cells(1, 5).Value With Sheets("Overview") LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row With .Range("E2:E" & LastRow) .Formula = "=VLOOKUP(B2," & str & "!B:F,5,FALSE)" End With End With End Sub