簡體   English   中英

VBA Excel輸入框運行時錯誤13類型不匹配

[英]VBA Excel inputbox run time error 13 type mismatch

我有一個運行良好的宏。 但是我遇到的問題是InputBox。 當用戶在InputBox中按下CANCEL或X時,宏運行出錯。 用戶最多可以輸入15個值進行搜索,並輸入0以開始搜索。 我想使其更強大,這樣我就不會遇到這些錯誤。 請幫忙。

Sub FindValues()    
    Dim LSearchRow As Integer
    Dim rw As Integer, cl As Range, LSearchValue As Long, LCopyToRow As Integer
    Dim iHowMany As Integer
    Dim aSearch(15) As Long
    Dim i As Integer

    ' clear the sheets before it runs so to accurate number of funds opend.

    Sheet2.Cells.ClearContents
    Sheets("tier 2").Cells.ClearContents
    Sheets("tier 3").Cells.ClearContents
    Sheets("tier 4").Cells.ClearContents
    Sheets("tier 5").Cells.ClearContents

    On Error GoTo Err_Execute
    FixC
    Sheet2.Cells.Clear
    Sheet1.Select
    iHowMany = 0
    LSearchValue = 99

    'this for the end user to input the required A/C to be searched

    Do While LSearchValue <> 0
        LSearchValue = InputBox("Please enter a value to search for. Enter a zero to indicate finished" & _
    "entry.", "Enter Search value")
        If LSearchValue <> 0 Then
            iHowMany = iHowMany + 1
            If iHowMany > 15 Then
                MsgBox "You are limited to 15 search numbers.", vbOKOnly, "Limit reached"
                iHowMany = 15
                Exit Do
            End If
            aSearch(iHowMany) = LSearchValue
        End If
    Loop

    If iHowMany = 0 Then
        MsgBox "No selections entered.", vbOKOnly + vbCritical, "No Search data"
        Exit Sub
    End If

    LCopyToRow = 2

    For rw = 1 To 1555
        For Each cl In Range("D" & rw & ":M" & rw)
        '------------------------------------------------
            For i = 1 To iHowMany
                Debug.Print cl.Row & vbTab & cl.Column
                LSearchValue = aSearch(i)
                If cl = LSearchValue Then
                    cl.EntireRow.Copy
                    'Destination:=Worksheets("Sheet2")
                    '.Rows(LCopyToRow & ":" & LCopyToRow)
                    Sheets("Sheet2").Select
                    Rows(LCopyToRow & ":" & LCopyToRow).Select
                    'Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
                    Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
                       xlNone, SkipBlanks:=False, Transpose:=False
                    'Move counter to next row
                    LCopyToRow = LCopyToRow + 1
                    'Go back to Sheet1 to continue searching
                    Sheets("Sheet1").Select
                End If
            Next i
            'LSearchRow = LSearchRow + 1
        Next cl
    Next rw
    'Position on cell A3
    'Application.CutCopyMode = False
    'Selection.Copy
    Sheets("Sheet2").Select
    Cells.Select
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False

    Application.CutCopyMode = False
    Sheet2.Select
    MsgBox "All matching data has been copied."
Exit Sub

將字符串變量Dim LSearchString as String定義Dim LSearchString as String並將其分配為InputBox的返回值: LSearchString = InputBox(...) 現在,您可以檢查輸入的數字: If IsNumeric(LSearchString)並處理錯誤的輸入。 如果成功,則可以將其轉換為整數: LSearchValue = CInt(LSearchString) 您必須知道,如果參數不是數字,則CInt()始終返回0。

以下代碼段是代碼的修改后的摘錄。 錯誤的輸入只是被忽略。

更新

Dim LSearchValue As Integer
Dim LSearchString As String

'..........

LSearchValue = 99

Do While True
    LSearchString = InputBox( _
           "Please enter a value to search for. " & _
           "Enter a zero to indicate finished entry", _
           "Enter Search value")

    If IsNumeric(LSearchString) Then
        LSearchValue = CInt(LSearchString)
        If LSearchValue = 0 Then Exit Do

        iHowMany = iHowMany + 1
        If iHowMany > 15 Then
            MsgBox "You are limited to 15 search numbers.", vbOKOnly, "Limit reached"
            iHowMany = 15
            Exit Do
        End If
        aSearch(iHowMany) = LSearchValue
    End If
Loop

如果要處理取消,請使用中間變體

Sub dural()
    Dim v As Variant, LSearchValue As Long
    v = InputBox("Please enter a value to search for. Enter a zero to indicate finished" & "entry.", "Enter Search value")
    If v = "" Then
        MsgBox "I guess you don't want to search"
    Else
        LSearchValue = CLng(v)
        MsgBox "I will search for " & LSearchValue
    End If
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM