+ Reply to Thread
Results 1 to 6 of 6

Looping Macro Across All Sheet with Exceptions

Hybrid View

  1. #1
    Registered User
    Join Date
    09-21-2015
    Location
    London
    MS-Off Ver
    2013
    Posts
    25

    Looping Macro Across All Sheet with Exceptions

    Hi,

    I've got some simple code, that I can get to work on one sheet quite well. It removes any rows with date in Column "K" that are over 180 days in the future. I need to run it across all the sheets in my workbook, with the exceptions of "Master", "Exceptions", "Unlimited" and "Closed"

    I've tried this:

    Sub Remove180Loop()
        Dim ws As Worksheet
        Dim i As Integer
        Dim Lastrow As Long, x As Long
        
            For Each ws In ActiveWorkbook.Worksheets
            
                    For i = 1 To Sheets.Count
                        If Worksheets(i).Name <> "Master" Or Worksheets(i).Name <> "Exceptions" Or _
                        Worksheets(i).Name <> "Unlimited" Or Worksheets(i).Name <> "Closed" Then
                            Lastrow = Cells(Cells.Rows.Count, "A").End(xlUp).row
                            For x = Lastrow To 5 Step -1
                                If Cells(x, "K").Value > Date + 180 Then Rows(x).Delete
                            Next
                        End If
                    Next I
    
          Next ws
          
    End Sub
    It still works on the first sheet, but won't loop through the book. Any help would be great.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Looping Macro Across All Sheet with Exceptions

    Try this.
    Sub Remove180Loop()
    Dim ws As Worksheet
    Dim i As Integer
    Dim Lastrow As Long, x As Long
    
        For Each ws In ActiveWorkbook.Worksheets
            With ws
                If .Name <> "Master" Or .Name <> "Exceptions" Or _
                   .Name <> "Unlimited" Or .Name <> "Closed" Then
                    Lastrow = .Cells(Cells.Rows.Count, "A").End(xlUp).Row
                    For x = Lastrow To 5 Step -1
                        If .Cells(x, "K").Value > Date + 180 Then .Rows(x).Delete
                    Next
                End If
            End With
        Next ws
    
    End Sub
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    09-21-2015
    Location
    London
    MS-Off Ver
    2013
    Posts
    25

    Re: Looping Macro Across All Sheet with Exceptions

    Perfect!!. Works a treat. Was nearly there!

  4. #4
    Registered User
    Join Date
    09-21-2015
    Location
    London
    MS-Off Ver
    2013
    Posts
    25

    Re: Looping Macro Across All Sheet with Exceptions

    Quote Originally Posted by gabbana View Post
    Perfect!!. Works a treat. Was nearly there!
    Spoke too soon, it's running on the excluded sheets.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Looping Macro Across All Sheet with Exceptions

    Change the Or(s) in the If statement to And(s).

  6. #6
    Registered User
    Join Date
    09-21-2015
    Location
    London
    MS-Off Ver
    2013
    Posts
    25

    Re: Looping Macro Across All Sheet with Exceptions

    Quote Originally Posted by Norie View Post
    Change the Or(s) in the If statement to And(s).
    I'd just worked that out! Thank you very much for your help.

+ 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] Looping Worksheet Macro - Skip a Sheet
    By tudley in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 02-03-2015, 12:32 AM
  2. [SOLVED] Reference every sheet (with exceptions) within a workbook
    By Kramxel in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-08-2013, 05:15 AM
  3. [SOLVED] Multiiple Worksheet Loop Macro only Looping on 1 sheet
    By lscarstens in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-20-2012, 02:44 PM
  4. Macro not looping through whole sheet
    By mjltigger in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-22-2010, 07:26 AM
  5. Protect sheet with exceptions
    By shatzia in forum Excel General
    Replies: 1
    Last Post: 09-22-2010, 03:40 PM
  6. Macro for filters with exceptions?
    By hiphopopotamus in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-20-2010, 07:44 AM
  7. looping macro to reformat data from one sheet to another
    By johngombola in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-18-2009, 12:08 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