Loop macro for all workbooks
Results 1 to 5 of 5

Thread: Loop macro for all workbooks
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular
    Join Date
    Jan 2011
    Location
    Midlands
    Posts
    1,154
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Loop macro for all workbooks

    Hello all

    I hope you can help on this please

    I want to use this line of code

    Cells.EntireColumn.AutoFit

    but I would like to run this for all the worksheets in my workbook

    I could select each workbook to do this but is there an easy way for the code to loop through all the workbooks please

  2. #2
    MrExcel MVP
    Junior Admin
    Joe4's Avatar
    Join Date
    Aug 2002
    Posts
    50,086
    Post Thanks / Like
    Mentioned
    52 Post(s)
    Tagged
    11 Thread(s)

    Default Re: Loop macro for all workbooks

    Like this:
    Code:
        Dim ws As Worksheet
        For Each ws In Worksheets
            ws.Activate
            Cells.EntireColumn.AutoFit
        Next ws
    TIPS FOR FINDING EXCEL SOLUTIONS
    1. Use the built-in Help that comes with Excel/Access
    2. Use the Search functionality on this board
    3. A lot of VBA code can be acquired by using the Macro Recorder.

    "Give a man a fish, feed him for a day. Teach a man to fish, feed him for life!"

  3. #3
    Board Regular
    Join Date
    Jan 2011
    Location
    Midlands
    Posts
    1,154
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Loop macro for all workbooks

    Thanks Joe

    One question please if I may

    I want run this on all worksheets except "Dashboard"
    How could I stop the code from running on "DashBoard" please

  4. #4
    MrExcel MVP
    Junior Admin
    Joe4's Avatar
    Join Date
    Aug 2002
    Posts
    50,086
    Post Thanks / Like
    Mentioned
    52 Post(s)
    Tagged
    11 Thread(s)

    Default Re: Loop macro for all workbooks

    Just add an IF block, i.e.
    Code:
        Dim ws As Worksheet
        For Each ws In Worksheets
            If ws.Name <> "Dashboard" Then
                ws.Activate
                Cells.EntireColumn.AutoFit
            End If
        Next ws
    TIPS FOR FINDING EXCEL SOLUTIONS
    1. Use the built-in Help that comes with Excel/Access
    2. Use the Search functionality on this board
    3. A lot of VBA code can be acquired by using the Macro Recorder.

    "Give a man a fish, feed him for a day. Teach a man to fish, feed him for life!"

  5. #5
    Board Regular My Aswer Is This's Avatar
    Join Date
    Jul 2014
    Posts
    15,683
    Post Thanks / Like
    Mentioned
    28 Post(s)
    Tagged
    13 Thread(s)

    Default Re: Loop macro for all workbooks

    Try this:
    Code:
    Sub Auto_Fit()
    Dim i As Long
        
        For i = 1 To Sheets.Count
        If Sheets(i).Name <> "Dashboard" Then
            Sheets(i).Cells.Columns.AutoFit
        End If
        Next
    End Sub
    Be sure and always test this script on sample data the first time you use this as to avoid any problems with your data. Always trying to learn more and help others where I can. I'm using Excel 2013.
    Patience please I’m not perfect yet. "Memories are forever"

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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