Compile error: Next Without For Error

Tonysdilemma

New Member
Joined
Aug 2, 2020
Messages
30
Office Version
  1. 2016
Platform
  1. Windows
Hi
Have a workbook with many sheets.
Have VBA to search through sheets and get result. Was hoping to get result to flash 3 times for 3 seconds.
However get the following error; Compile error: Next Without Error. Can someone help? Below is the code. The Green Text in the code is where I believe the error is occuring

VBA Code:
Sub FindMe()
Dim MySearch As Variant
Dim X As Integer

MySearch = InputBox("Please enter a value below, and hit OK to search", _
    "What are you looking for?")

Workbooks("brutus.xlsm").Activate
Range("A1").Select

For X = 1 To Sheets.Count
    On Error Resume Next
      
    Sheets(X).Select
      
    Cells.Find(What:=MySearch, After:=ActiveCell, LookIn:=xlValues, LookAt _
            :=xlPart, SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, MatchCase:= _
                    False, SearchFormat:=False).Activate
        
 [COLOR=rgb(247, 218, 100)] [/COLOR][COLOR=rgb(65, 168, 95)]    If InStr(1, ActiveCell.Value, MySearch, vbTextCompare) Then
    
       Dim d As Date
     d = Now
    
     Application.OnTime d + TimeValue("00:00:03"), "ChangeCellColor"
    
     i = i + 1
       Range(ActiveCell).Interior.ColorIndex = Choose(i, 3, 0, 3, 0, 3, 0)
       If i = 6 Then
      i = 0
    
    
       Application.OnTime d + TimeValue("00:00:03"), "ChangeCellColor", , False
      
    End
      
    End If
Next X



MsgBox "Sorry " & MySearch & " was not found in any sheet", vbOKOnly, "No Match!"


End Sub[/COLOR]
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
The problem is fixed by changing this:
End

For this:
End IF
 
Upvote 0
But your macro has other problems. Explain what you need to do and also put here the code of the "ChangeCellColor" macro

Note, to put colors or formats in the code use the Rich Code Tag:

Rich (BB code):
Sub macro()
  Dim i As Long
  
  For i = 1 To 5
    MsgBox i
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,336
Messages
6,124,328
Members
449,155
Latest member
ravioli44

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top