【问题标题】:VBA Excel inputbox run time error 13 type mismatchVBA Excel输入框运行时错误13类型不匹配
【发布时间】:2015-02-14 12:14:29
【问题描述】:

我有一个运行良好的宏。但我遇到的问题是 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

【问题讨论】:

    标签: excel vba input vb6


    【解决方案1】:

    定义一个字符串变量Dim LSearchString as String,并将你的InputBox的返回值赋值给它:LSearchString = InputBox(...)。现在您可以检查输入是否为数字:If IsNumeric(LSearchString) 并处理错误的输入。如果成功,您可以继续将其转换为整数:LSearchValue = CInt(LSearchString)。你要知道CInt()在参数不是数字时总是返回0。

    以下代码 sn-p 是从您的代码中修改后的摘录。 错误的输入会被忽略。

    更新

    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
    

    【讨论】:

    • 您好,谢谢。你能把它写成代码吗?我会真的很喜欢它
    • 你能把这个放到脚本里吗?一直在尝试,但它不适合我
    • @kay:可能是这样的(见更新)。您必须用输入框替换部分代码。
    • 看起来不错,只是遇到了溢出问题。但是我问的问题很好用,谢谢。将向您发布有关溢出问题的信息,现在正在处理。很棒的社区
    • 我修复了溢出问题,只是将变量从整数更改为长整数。这又必须是 Clng。非常感谢这个真正的帮助
    【解决方案2】:

    如果你想处理一个Cancel,使用一个中间Variant

    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
    

    【讨论】:

    • 完美适用于我的取消按钮.. 太棒了。如果用户 X 退出,我会遇到同样的错误。
    • 这段代码有效,当用户按下取消按钮时,我一直试图退出,而不是再次弹出输入框
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多