-2

When running this code I get a error that is "loop without do". I want if "case vbno" is selected then it returns to the original input box. If it the user selects "case vbyes" I want it to highlight then cell then loop back to do and return to original input box. If cancel is selected I want it to exit completely.

Sub find_highlight3()
    Dim w As Variant
    Dim FoundCell As Range
    Dim ans As String

    Do

        w = InputBox("What to find?")

        Cells.Find(What:=(w), After:=ActiveCell, LookIn:=xlFormulas, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False).Activate
        With Selection.Interior

            Select Case MsgBox("Hellow", vbYesNoCancel)

                Case vbNo

    Loop

                Case vbYes

                   .ColorIndex = 6
                   .Pattern = xlSolid
                   .PatternColorIndex = xlAutomatic

    Loop

                Case vbCancel

            End Select

        End With
End Sub
Community
  • 1
  • 1
Mitch
  • 1
  • 1

1 Answers1

0

The following code should do what you want, while still maintaining the integrity of each "block" of code.

Sub find_highlight3()
    Dim w As Variant
    Dim FoundCell As Range
    Dim ans As String

    Do
        w = InputBox("What to find?")

        Cells.Find(What:=(w), After:=ActiveCell, LookIn:=xlFormulas, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False).Activate

        Select Case MsgBox("Hellow", vbYesNoCancel)

            Case vbNo

            Case vbYes    
                With Selection.Interior
                    .ColorIndex = 6
                    .Pattern = xlSolid
                    .PatternColorIndex = xlAutomatic
                End With

            Case vbCancel    
                Exit Do

        End Select
    Loop
End Sub

Note: Your Activate statement will fail if the Find does not match anything (because Nothing.Activate is invalid), but that is a question for another day.

YowE3K
  • 23,852
  • 7
  • 26
  • 40