用VBA将vlookup函数写入Excel单元格
我很难理解为什么下面的代码不起作用?我是一个缺少什么的人用VBA将vlookup函数写入Excel单元格,excel,vba,vlookup,Excel,Vba,Vlookup,我很难理解为什么下面的代码不起作用?我是一个缺少什么的人 Sub vlookup_test() Dim class As String class = "tmp_1" Cells(1, 1).Formula = "=vlookup(" & class & ",'Sheet1'!A:B,2,FALSE)" End Sub 我修改了代码,以便更清楚地了解子系统 Sub vlookup_test() Dim class As String Dim rngSe
Sub vlookup_test()
Dim class As String
class = "tmp_1"
Cells(1, 1).Formula = "=vlookup(" & class & ",'Sheet1'!A:B,2,FALSE)"
End Sub
我修改了代码,以便更清楚地了解子系统
Sub vlookup_test()
Dim class As String
Dim rngSearch As Range
class = "tmp_1"
'Create a with statement for the worksheet of your interest
With ThisWorkbook.Worksheets("Sheet1")
'Set the range to search in
Set rngSearch = .Range("C:D")
'Check if value found
If IsError(Application.VLookup(class, rngSearch, 2, False)) = False Then
'Found
.Cells(1, 1).Value = Application.VLookup(class, rngSearch, 2, False)
Else
'Not found
.Cells(1, 1).Value = "Not Found"
End If
End With
End Sub
这将修复现有代码:
Cells(1,1)。Formula=“=VLOOKUP(“&class&”“,'Sheet1'!A:B,2,FALSE)”
是范围的名称还是文字值?如果是后者,则需要用引号括起来:Cells(1,1)。公式=“=vlookup(“&class&”“,'Sheet1'!A:B,2,FALSE)”
这是一个文本!谢谢你,它工作得很好!:)