+ Reply to Thread
Results 1 to 11 of 11

Macro Loop through all sheets

  1. #1
    Registered User
    Join Date
    06-07-2013
    Location
    lahnam md
    MS-Off Ver
    Excel 2010
    Posts
    15

    Macro Loop through all sheets

    I have a macro that currently only runs on one sheet. I have about 50 sheets that I need this to run through in the same workbook. Any ideas on how to make it run all sheets?


    This is the macro:


    Sub Delete_Columns1()
    Dim rng As Range, cell As Range, del As Range
    Set rng = Intersect(Range("C4:AW4”), ActiveSheet.UsedRange)
    For Each cell In rng
    If (cell.Value) = "BENCHMARK" _
    Then
    If del Is Nothing Then
    Set del = cell
    Else: Set del = Union(del, cell)
    End If
    End If
    Next cell
    On Error Resume Next
    del.EntireCOLUMN.Delete
    End Sub


    As always, thanks for all your help!

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Macro Loop through all sheets

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    06-07-2013
    Location
    lahnam md
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Macro Loop through all sheets

    Thanks for the reply- there is an error with the following

    Dim rng As Range, cell As Range, del As Range, ws

    compile error: syntax error

  4. #4
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Macro Loop through all sheets

    Works fine for me. You could try changing it to:
    Please Login or Register  to view this content.
    I suppose but I doubt it will make any difference.

  5. #5
    Registered User
    Join Date
    06-07-2013
    Location
    lahnam md
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Macro Loop through all sheets

    Sorry, error is with

    For Each cell In rng

  6. #6
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Macro Loop through all sheets

    Do all of your sheets have some data in C4:AW4?

    It won't work on an empty sheet for example, maybe try:
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    06-07-2013
    Location
    lahnam md
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Macro Loop through all sheets

    It worked on the active sheet then gave error


    Method "Union of object'_Global' failed.

    Not all sheets are identical

  8. #8
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Macro Loop through all sheets

    Sorry, don't know what the problem is, it will run the same macro on each sheet so I would guess there is something in your data on the other sheets that is causing a problem. Can you upload your workbook then I can take a better look.

  9. #9
    Registered User
    Join Date
    06-07-2013
    Location
    lahnam md
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Macro Loop through all sheets

    Data has been deleted but the headings and format are the same.

    Also, there are about 30 more sheets in the actual file that roughly follow the same pattern- I had to delete them because the file was too large.


    Thanks.
    Attached Files Attached Files

  10. #10
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Macro Loop through all sheets

    Sorry, my mistake. the del variable needs clearing on each loop through:
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    06-07-2013
    Location
    lahnam md
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Macro Loop through all sheets

    Thank you very much for your help!!! It works beautifully!

+ 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] Trouble with macro - Loop through all work sheets
    By RichTea88 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 02-06-2013, 05:23 AM
  2. Loop Macro with different Sheets
    By Jiptastic in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-05-2012, 06:30 PM
  3. Macro to loop in all sheets ...Please Help
    By gokzee in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-29-2012, 02:46 AM
  4. Loop macro to run on each of several sheets in a workbook...
    By jwhitwell in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-29-2008, 10:43 AM
  5. Macro to loop through all sheets
    By erock24 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-12-2007, 11:57 AM

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