Я изменил код, чтобы дать более четкое представление о сабвуфере.
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)"