Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用VBA将vlookup函数写入Excel单元格_Excel_Vba_Vlookup - Fatal编程技术网

用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)”
这是一个文本!谢谢你,它工作得很好!:)