0

I have a macro which is working well. But the issue I have is with the InputBox. The macro is running into errors when the user presses CANCEL or X out of the InputBox. What can I do to stop this from happening. If the user presses X or Cancel the process should end, ie. exit the sub. My code is below:

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

1 Answer 1

2

You need to check if LSearchValue is empty. Use this do loop

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 = "" Then
        Exit Do '<~~ Or Exit Sub if you want to terminate the sub
    ElseIf 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
    End If
Loop
Sign up to request clarification or add additional context in comments.

6 Comments

If OP wants to terminate the routine on Cancel Exit Sub will suit well as pointed out in the question.
Siddharth i am getting an error of "13 type mismatch" with that. I just trying to make the code more robust. It wont be professional to have an error occur for the user. I have also tried error handlers , no success.
@kay: Change Dim LSearchRow As Integer to Dim LSearchRow As Variant
Na no success , still searching for the answer . I have tried different variations of it. Maybe the input my not be most appropriate for this situation.
@Kay: It works for me. Are you sure you are using it as I have mentioned above?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.