+ Reply to Thread
Results 1 to 10 of 10

Running macro on all worksheets in a book

  1. #1
    Forum Contributor
    Join Date
    02-20-2007
    Location
    Alaska
    MS-Off Ver
    2003, 2007, 2010
    Posts
    105

    Running macro on all worksheets in a book

    I have a macro that I would like to run on every worksheet in my book (over 100 sheets wide). As it takes about 20 secs to run on each sheet, and I don't want to click from sheet to sheet, how can I make it run on every sheet in the book? And no, they aren't named sequentially, either alphabetically or numerically.

    Also, is there a function in Excel to return the number of sheets in a book?

    Thanks!
    F6Hawk

  2. #2
    Forum Contributor
    Join Date
    02-20-2007
    Location
    Alaska
    MS-Off Ver
    2003, 2007, 2010
    Posts
    105
    Never mind on the counting sheets part, I found that via search. Sorry!

  3. #3
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,937
    You may be able to wrap your macro code in something like this:
    PHP Code: 
    Public Sub test()
         
    Dim wb As Object
         Dim sh
         Set wb 
    Workbooks("book1")
         For 
    Each sh In wb.Sheets
              MsgBox sh
    .Name
         Next sh
    End Sub 
    Ben Van Johnson

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    protonLeah's code will do just what you want. I would modify it to declare object variables by type for better performance, and so that Intellisense works:
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    02-20-2007
    Location
    Alaska
    MS-Off Ver
    2003, 2007, 2010
    Posts
    105
    Well, I tried that (at least, I thought I did, with my code below), but all it did was run the code over & over on the first sheet.

    Please Login or Register  to view this content.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    You can either modify your macro to reference the correct sheet (ws.Range(...), or do
    Please Login or Register  to view this content.
    before calling your macro.

    Edit: BTW, that code will error if you have chart sheets.
    Last edited by shg; 10-08-2007 at 01:09 AM.

  7. #7
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243
    hi F6Hawk,

    Do you have any code in place to try & minimise the time of 20 seconds per sheet (eg "application.screenupdating = false" &/or "Application.Calculation = xlCalculationManual")?

    VbNoob recently provided the following link to Charles Williams' below link which has some good suggestions for optimising speed
    Please Login or Register  to view this content.
    in the below thread

    http://excelforum.com/showthread.php?t=617075


    To overcome the possibility of errors mentioned by SHG you can change "For Each wS In sheets" to "For Each wS In Worksheets" as this ignores Chart sheets.

    hth
    Rob
    Last edited by VBA Noob; 10-08-2007 at 03:24 AM.
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  8. #8
    Forum Contributor
    Join Date
    02-20-2007
    Location
    Alaska
    MS-Off Ver
    2003, 2007, 2010
    Posts
    105
    The problem is that I have 186 sheets, and calling them by name will be a pain in the...

    I guess I am missing something here, becaue thus far, all these do is run my macro repeatedly on the SAME sheet, it does not select subsequent sheets to run the macro on.

    Quote Originally Posted by shg
    You can either modify your macro to reference the correct sheet (ws.Range(...), or do
    Please Login or Register  to view this content.
    before calling your macro.

    Edit: BTW, that code will error if you have chart sheets.

  9. #9
    Forum Contributor
    Join Date
    02-20-2007
    Location
    Alaska
    MS-Off Ver
    2003, 2007, 2010
    Posts
    105
    Ok, I think I got it now...


    Please Login or Register  to view this content.
    This one runs my RunAll macro on a sheet, selects the next sheet, runs it, and so on.

    Thanks for all the help, folks!

    F6

  10. #10
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243
    Thanks for the feedback, I'm pleased we could help :-)

    Just out of curiosity, does it still take 20sec per sheet to run?

    Rob

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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