+ Reply to Thread
Results 1 to 4 of 4

Repeat Macro for multiple Sheets - Next without For

Hybrid View

  1. #1
    Registered User
    Join Date
    10-10-2013
    Location
    New Plymouth, New Zealand
    MS-Off Ver
    Excel 2010
    Posts
    2

    Repeat Macro for multiple Sheets - Next without For

    I have multiple sheets with the same layout some have different data. My code goes to each sheet and filters on criteria then copies to another sheet. However, if there is no data it copy it gets stuck.
    I tried putting in an If IsEmpty line so the loop would move to the next sheet but I keep getting a compile error: Next without For.

    I have been searching for hours but am getting no closer to a fix. Here is my code:

      Dim sheet_name As Range
      For Each sheet_name In Sheets("sheet3").Range("E:E")
        If sheet_name.Value = "" Then
            Exit For
    Else
        Sheets(sheet_name.Value).Select
        Rows("1:1").Select
        Selection.AutoFilter
        ActiveSheet.Range("$A:$AH").AutoFilter Field:=5, Criteria1:="SBHS Yr 9"
        Rows("2:2").Select
        Range(Selection, Selection.End(xlDown)).Select
        If IsEmpty(sheet_name.Value) = True Then
        Selection.AutoFilter
        End If
        Next sheet_name
    Else
        If IsEmpty(sheet_name.Value) = False Then
        Selection.Copy
        Sheets("Year 9 Waitara").Select
        lMaxRows = Cells(Rows.Count, "a").End(xlUp).Row
        Range("a" & lMaxRows + 1).Select
        ActiveSheet.Paste
        Sheets(sheet_name.Value).Select
        Selection.AutoFilter
       End If
    Next sheet_name
    End Sub
    Last edited by Fotis1991; 10-14-2013 at 04:03 AM. Reason: added code tags in this post too!!

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Repeat Macro for multiple Sheets - Next without For

    Please use code tags with your code as per forum's rule.
    Dim sheet_name As Range
     For Each sheet_name In Sheets("sheet3").Range("E:E")
    Is looping through a range in sheet 3, as sheet_name is a range, not a sheet

    You need
    Dim sheet_name worksheet
     For Each sheet_name In Sheets

  3. #3
    Registered User
    Join Date
    10-10-2013
    Location
    New Plymouth, New Zealand
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Repeat Macro for multiple Sheets - Next without For

    Thank you for your advise and sorry for my error.

    I have changed the code as you suggested but I am still getting the error. I have added a line to my code below to show where the error is triggered. I have tried changing the order but get the same compile error.


     Dim sheet_name As Worksheet
      For Each sheet_name In Sheets("sheet3").Range("E:E")
        If sheet_name = "" Then
            Exit For
    Else
        Sheets(sheet_name).Select
        Rows("1:1").Select
        Selection.AutoFilter
        ActiveSheet.Range("$A:$AH").AutoFilter Field:=5, Criteria1:="SBHS Yr 9"
        Rows("2:2").Select
        Range(Selection, Selection.End(xlDown)).Select
     'check if data in worksheet after filter
        If IsEmpty(sheet_name) = True Then
        Selection.AutoFilter
        End If
        Next sheet_name
    '***compile error - next without for***
    Else
        If IsEmpty(sheet_name.Value) = False Then
        Selection.Copy
        Sheets("Year 9 Waitara").Select
        lMaxRows = Cells(Rows.Count, "a").End(xlUp).Row
        Range("a" & lMaxRows + 1).Select
        ActiveSheet.Paste
        Sheets(sheet_name.Value).Select
        Selection.AutoFilter
       End If
    Next sheet_name
    End Sub

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Repeat Macro for multiple Sheets - Next without For

    It appears to me there are few errors on the code and still more errors to be found.

    Sub test()
     Dim sheet_name As Worksheet
      For Each sheet_name In Sheets("sheet3").Range("E:E")
        If sheet_name = "" Then
            Exit For
    Else
        Sheets(sheet_name).Select
        Rows("1:1").Select
        Selection.AutoFilter
        ActiveSheet.Range("$A:$AH").AutoFilter Field:=5, Criteria1:="SBHS Yr 9"
        Rows("2:2").Select
        Range(Selection, Selection.End(xlDown)).Select
        End If
     'check if data in worksheet after filter
        If IsEmpty(sheet_name) = True Then
        Selection.AutoFilter
        End If
        'Next sheet_name
    '***compile error - next without for***
    
        If IsEmpty(sheet_name) = False Then
        Selection.Copy
        Sheets("Year 9 Waitara").Select
        lMaxRows = Cells(Rows.Count, "a").End(xlUp).Row
        Range("a" & lMaxRows + 1).Select
        ActiveSheet.Paste
        Sheets(sheet_name).Select
        Selection.AutoFilter
       End If
    Next sheet_name
    End Sub
    This should give you some idea on how to loop through sheet collection


    Sub test1()
     Dim sheet_name As Worksheet
      For Each sheet_name In ThisWorkbook.Worksheets
      
        With sheet_name
            If .Name = "" Then
                Exit For
            Else
                
                .Rows(1).AutoFilter
                .Range("$A:$AH").AutoFilter Field:=5, Criteria1:="SBHS Yr 9"
                .Rows("2:2").Select
                .Range(Selection, Selection.End(xlDown)).Select
                End If
             'check if data in worksheet after filter
                If IsEmpty(sheet_name) = True Then
                Selection.AutoFilter
                End If
                'Next sheet_name
            '***compile error - next without for***
            
                If IsEmpty(sheet_name) = False Then
                Selection.Copy
                Sheets("Year 9 Waitara").Select
                lMaxRows = Cells(Rows.Count, "a").End(xlUp).Row
                Range("a" & lMaxRows + 1).Select
                ActiveSheet.Paste
                Sheets(sheet_name).Select
                Selection.AutoFilter
               End If
        End With
            Next sheet_name
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] How to repeat a macro in multiple rows of a sheet?
    By rgoodman in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 07-01-2013, 09:07 AM
  2. [SOLVED] Copy multiple ranges to different sheets and repeat on button click
    By ooze76 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-13-2012, 05:46 PM
  3. Excel 2007 : Repeat Columns in multiple sheets
    By jbode in forum Excel General
    Replies: 1
    Last Post: 01-04-2011, 04:39 PM
  4. macro to repeat on majority but not all other sheets in workbook
    By raehippychick in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-18-2007, 09:15 AM
  5. VBA Code To have a macro repeat on all sheets in a workbook
    By carl in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 11-03-2005, 03:50 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1