Results 1 to 5 of 5

Thread: Run Macro on multiple worksheets within an excel file

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    2

    Run Macro on multiple worksheets within an excel file

    Hello,

    I am trying to create a macro to run on all worksheets within a tab. So far it is only running on one worksheet and it does not work on other. Can someone please assist me. Below is the code I used. Thank you

    Code:
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
    If ws.Name <> "Sheet1" Or ws.Name <> "Sheet2" Then
    Columns("F:H").Select
    Range("H1").Activate
    Selection.EntireColumn.Hidden = True
    End If
    
    Next ws.Activate
    End Sub
    Last edited by Siddharth Rout; Apr 27th, 2013 at 01:17 PM. Reason: Added Code Tages

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Run Macro on multiple worksheets within an excel file

    Welcome to the forums biyi11

    Is this wihat you are trying?

    Code:
        Sub Sample()
            Dim ws As Worksheet
            
            For Each ws In ThisWorkbook.Worksheets
                If ws.Name <> "Sheet1" Or ws.Name <> "Sheet2" Then
                    ws.Columns("F:H").EntireColumn.Hidden = True
                End If
            Next
        End Sub
    Last edited by Siddharth Rout; Apr 28th, 2013 at 11:04 PM. Reason: typo fixed
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    2

    Re: Run Macro on multiple worksheets within an excel file

    Yes, but it seems not be working on all the tabs in the excel tab. Do I need an activate all worksheets statement included.



    Quote Originally Posted by koolsid View Post
    Welcome to the forums biyi11

    Is this wihat you are trying?

    Code:
        Sub Sample()
            Dim ws As Worksheet
            
            For Each ws In ThisWorkbook.Worksheets
                If ws.Name <> "Sheet1" Or ws.Name <> "Sheet2" Then
                    ws.Columns("F:H").EntireColumn.Hidden = True
                End If
            End Sub
        End Sub

  4. #4
    Hyperactive Member
    Join Date
    Oct 2007
    Location
    Godzone, oops Oz
    Posts
    355

    Re: Run Macro on multiple worksheets within an excel file

    Your first End Sub should be a next statement

  5. #5
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Run Macro on multiple worksheets within an excel file

    Thanks kiwi. typo fixed.

    Try it now biyi11
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

Posting Permissions

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



Click Here to Expand Forum to Full Width