我正在尝试为工作表创建一个vlookup,其名称与我的活动工作表中的单元格值相同。
我创造了以下想法,我可以使用' str'在我的vlookup公式中,我得到了运行时错误' 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
谁能看到我做错了什么?
答案 0 :(得分:2)
您已在VBA中定义str
,但在公式中将其引用而未关闭引号,请尝试以下操作:
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