Consulting

Results 1 to 4 of 4

Thread: How to run macro for multiple worksheets

  1. #1
    VBAX Newbie
    Joined
    Jan 2019
    Posts
    1
    Location

    Question How to run macro for multiple worksheets

    Hello,
    I have almost 25 different worksheets in one excel file. Now in order to format it I have created a macro for each worksheet. If I go to each worksheet and run the macro for that worksheet it works fine.
    Now I'm trying to automate this entire process, run all these macros for the respective worksheet. Can someone please guide or provide me a script that will help me to achieve this task

    Any help will be appreciated.

    Thank you.

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Welcome to the forum!

    I would write the FormatSheet to take an input parameter such as sheet name or sheet object.

    If it just acts on the ActiveSheet and say skip sheet named Main, I guess you could do:

    Sub Main()  Dim wc As Worksheet, ws As Worksheet
      Set wc = ActiveSheet
      For Each ws In Worksheets
        If ws.Name <> "Main" Then
          ws.Activate
          FormatSheet
        End If
      Next ws
      wc.Activate
    End Sub
    
    
    Sub FormatSheet()
      [A1].NumberFormat = "mm/dd/yyyy"
    End Sub

  3. #3
    VBAX Contributor
    Joined
    Jul 2017
    Location
    Zurich
    Posts
    132
    Location
    I would avoid activating each of the sheets like so for example:

    Sub Main()  
      dim ws As Worksheet
      For Each ws In ThisWorkbook.Worksheets
        ws.Range("A1").NumberFormat = "mm/dd/yyyy"
      next ws
    End Sub
    Or if you want the foratting in a separate sub like so:

    Sub Main() 
      dim ws As Worksheet
      For Each ws In ThisWorkbook.Worksheets
        FormatSheets ws
      Next ws
    End Sub
    
    Sub FormatSheets(ws as Worksheet)
      ws.Range("A1").NumberFormat = "mm/dd/yyyy"
    End Sub

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Quote Originally Posted by dax007 View Post
    Hello,
    I have almost 25 different worksheets in one excel file. Now in order to format it I have created a macro for each worksheet. If I go to each worksheet and run the macro for that worksheet it works fine.
    Now I'm trying to automate this entire process, run all these macros for the respective worksheet. Can someone please guide or provide me a script that will help me to achieve this task

    Any help will be appreciated.

    Thank you.

    Is it the same basic macro for each of the 25 sheets with only the sheet name different, or are there different macros for different sheets?
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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