Next Without For Combile Error - For a loop

JoeRooney

Board Regular
Joined
Nov 27, 2017
Messages
171
Office Version
  1. 365
Hi All,

I am receiving a compile error “Next without For” for a loop I am trying tocreate.


I need the codeto loop through all the spreadsheets in a workbook, search for the word investigate in column W,if found copy cell A2 and paste the value into Column B in the “Master” spreadsheetin descending order.

If “investigate”is not found ignore and move onto the next sheet.

Below is the codeI have so far.

Any help isgreatly appreciated


Code:
Sub CombineData3()
Dim Sht As Worksheet
For Each Sht In ActiveWorkbook.Worksheets
If Sht.name <> "Master" Then
Sht.Select
Dim foundsomething As Range
    Dim searchterm As String
    searchterm = "Investigate"
On Error Resume Next
    ActiveSheet.ShowAllData
    Columns("A:W").Select
    Selection.EntireColumn.Hidden = False
    Set foundsomething = Application.ActiveSheet.Find(What:="Investigate", After:=ActiveCell, lookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False)
        
    If (Not foundsomething Is Nothing) And columnW = "Investigate" Then
        
        Range("A2").Copy
    Worksheets("Master").Activate
    Range("B1" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        
        Else
Sht.Select
Next Sht
End If
End Sub
 
Last edited by a moderator:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
It's a confusing message, it looks like you're missing an End If somewhere (or an Else).

That being said, your "Next" is also part of an IF statement it doesn't start in...?

Rough guessish, needs to be something more like...

Code:
Sub CombineData3()
Dim Sht As Worksheet
Dim foundsomething As Range
Dim searchterm As String

For Each Sht In ActiveWorkbook.Worksheets
    If Sht.Name <> "Master" Then
        Sht.Select
        searchterm = "Investigate"
        On Error Resume Next
        ActiveSheet.ShowAllData
        Columns("A:W").Select
        Selection.EntireColumn.Hidden = False
        Set foundsomething = Application.ActiveSheet.Find(What:="Investigate", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False)
    [COLOR=#ff0000]End If[/COLOR]

    If (Not foundsomething Is Nothing) And columnW = "Investigate" Then
        Range("A2").Copy
        Worksheets("Master").Activate
        Range("B1" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Else
        Sht.Select
    End If

[COLOR=#ff0000]Next Sht[/COLOR]

End Sub
 
Last edited:
Upvote 0
Another possibility
Code:
   Sub CombineData3()
   Dim Sht As Worksheet
   For Each Sht In ActiveWorkbook.Worksheets
      If Sht.name <> "Master" Then
         Sht.Select
         Dim foundsomething As Range
         Dim searchterm As String
         searchterm = "Investigate"
         On Error Resume Next
         ActiveSheet.ShowAllData
         Columns("A:W").Select
         Selection.EntireColumn.Hidden = False
         Set foundsomething = Application.ActiveSheet.Find(What:="Investigate", After:=ActiveCell, lookIn:=xlFormulas, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False)
         
         If (Not foundsomething Is Nothing) And columnW = "Investigate" Then
            Range("A2").Copy
            Worksheets("Master").Activate
            Range("B1" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
               :=False, Transpose:=False
         End If
      End If
      
   Next Sht
End Sub
 
Upvote 0
Perhaps.
Code:
Sub CombineData3()
Dim Sht As Worksheet
Dim searchterm As String
Dim Res As Variant

    searchterm = "Investigate"
    
    For Each Sht In ActiveWorkbook.Worksheets
    
        If Sht.Name <> "Master" Then
        
            On Error Resume Next
            
            With Sht

                .ShowAllData

                .Columns("A:W").EntireColumn.Hidden = False

                Res = Application.Match("searchterm", .Range("W:W"),0)
        
                 If Not IsError(Res) Then
                     .Range("A2").Copy
                     Worksheets("Master").Range("B" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
                End If

            End With
            
        End If
        
    Next Sht
    
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,300
Members
452,633
Latest member
DougMo

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