参数不是可选的-简单的VBA功能

时间:2018-08-01 16:03:15

标签: vba excel-vba

我在vba中不断收到Argument而不是可选的编译器错误,无法弄清原因。

我看不到任何错字,我什至没有任何参数,并且该子例程仅在单击按钮时执行。

Public Sub SearchAMLinesForIDs_Click()
    Dim time As String
    time = "AM"

    Dim end_row As Long
    end_row = Range("A2").End(xlDown).row


    Dim lookup_end_row As Long
    Dim lookup_result As Long

    lookup_end_row = ThisWorkbook.Sheets(time & "_Lines").Range("A2").End(xlDown).row

    lookup_table = ThisWorkbook.Sheets(time & "_Lines").Range("A2:C" & lookup_end_row)

    Dim row As Long

    For row = 1 To end_row
        lookup_result = WorksheetFunction.VLookup(Range("A" & row), lookup_table, 1, False)

        Cells(row + 1, 2) = lookup_result
    Next row

End Sub

3 个答案:

答案 0 :(得分:1)

如果遇到此类问题,请使用对象浏览器。

enter image description here

您可以在工作表的单元格中测试您的vlookup以验证有效性。

答案 1 :(得分:1)

Public Sub SearchAMLinesForIDs_Click()

    Dim sTime As String, shtData As Worksheet, sht As Worksheet
    Dim lookup_result, rngLookup As Range
    Dim rw As Long

    sTime = "AM"
    Set shtData = ThisWorkbook.Worksheets(sTime & "_Lines")
    Set sht = ActiveSheet

    Set rngLookup = shtData.Range(shtData.Range("A2"), shtData.Cells(shtData.Rows.Count, 1).End(xlUp))

    For rw = 2 To sht.Range("A2").End(xlDown).row

        'don't use Worksheetfunction, but test the return value for errors...
        lookup_result = Application.VLookup(sht.Cells(rw, 1), rngLookup, 1, False)

        sht.Cells(rw, 2) = IIf(IsError(lookup_result), "NoMatch!", lookup_result)

    Next rw

End Sub

答案 2 :(得分:0)

您的括号顺序错误。

lookup_result = WorksheetFunction.VLookup(Cells(1 + Row, 1), _
                                          ThisWorkbook.Sheets(Time & "_Lines").Range(Cells(2, 1), Cells(lookup_end_row, 1)), _
                                          1, _
                                          False)

打开范围的括号没有关闭,最后一直关闭。