ByRef Argument Type调用函数时不匹配

时间:2015-07-07 20:51:43

标签: vba excel-vba excel

我正在编写小型VBA代码,并在ByRef Argument Type Mismatch调用函数LoopThroughNamesAndPopulateValues()时收到错误Main()

Sub Main()
    Call LoopThroughQuoteNamesAndPopulateValues("Sheet2", 1, 1)
End Sub

Sub LoopThroughQuoteNamesAndPopulateValues(ByVal sheetName As String, ByVal row As Integer, ByVal column As Integer)
    Dim sheet As Excel.Worksheet
    Set sheet = ThisWorkbook.Sheets(sheetName)

    For i = row To 10000
        If sheet.Cells(i, column).Value = "" Then
            Exit For
        Else
            Call Finder_Get_Query(sheetName, i, column)
        End If
    Next i
End Sub

1 个答案:

答案 0 :(得分:0)

选项明确:添加"选项明确"到模块的顶部并编译。我的猜测是这个代码不会被编译,因为我没有被声明。也许不是问题,除非Finder_Get_Query窒息我。

错误处理:在Finder_Get_Query中放置一个错误处理程序,在LoopThroughNamesAndPopulateValues中放置一个错误处理程序,看看哪一个捕获了您的错误。这将有助于您缩小代码范围。

通过以下两个步骤可以解决许多简单的问题。这是我首选的错误处理程序,它可以方便调试,因为它允许您在运行时恢复,如果您需要:

Sub SampleErrorHandler()

    On Error GoTo EH

    'code

    GoTo FINISH

EH:
    With Err
        MsgBox "Error:" & vbTab & .Number & vbCrLf _
            & "Source" & vbTab & .Source & vbCrLf _
            & .Description
    End With

    'for debugging purposes
    Debug.Assert 0
    GoTo FINISH
    Resume

FINISH:

    'clean up, release resources

End Sub