+ Reply to Thread
Results 1 to 6 of 6

Macro to group / ungroup columns on multiple sheets

Hybrid View

yllew Macro to group / ungroup... 04-19-2018, 07:22 AM
vcoolio Re: Macro to group / ungroup... 04-19-2018, 08:21 AM
Arkadi Re: Macro to group / ungroup... 04-19-2018, 08:53 AM
yllew Re: Macro to group / ungroup... 05-01-2018, 09:46 AM
Arkadi Re: Macro to group / ungroup... 05-01-2018, 10:58 AM
vcoolio Re: Macro to group / ungroup... 05-01-2018, 06:40 PM
  1. #1
    Registered User
    Join Date
    06-03-2016
    Location
    Malaysia
    MS-Off Ver
    MS365
    Posts
    60

    Macro to group / ungroup columns on multiple sheets

    I have a workbook with approx. 15 sheets.
    First 10 sheets are having the same layout.
    I would like to group following columns on first 10 sheets:

    D,E,F (1 group)
    O,P,Q (1 group)
    Z,AA,AB (1 group)

    I have this code, but it will select all sheets in the workbook, how can I modify to select the first 10 sheets only?
    Sub Group_Sheets()
    
    Dim ws As Worksheet
    
    For Each ws In Worksheets
       ws.Select
       Columns("D:F").Group
       Columns("O:Q").Group
       Columns("Z:AB").Group
    Next ws
    
    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    06-29-2014
    Location
    Australia
    MS-Off Ver
    MSO 365
    Posts
    1,111

    Re: Macro to group / ungroup columns on multiple sheets

    Hello Yllew,

    Try modifying your code as follows:-

    Sub Group_Sheets()
    
        Dim sht As Worksheet, ws As Worksheet
        Dim shRng As Range, lr As Long
        
        Set sht = Sheets("List")
        lr = sht.Range("A" & Rows.Count).End(xlUp).Row
        Set shRng = sht.Range("A1:A" & lr)
        
    Application.ScreenUpdating = False
    
    For Each ws In Worksheets
            If shRng.Find(What:=ws.Name, LookAt:=xlWhole) Is Nothing Then '----> This line will exclude the unrequired sheets.
            ws.Columns("D:F").Group
            ws.Columns("O:Q").Group
            ws.Columns("Z:AB").Group
            End If
    Next ws
    
    Application.ScreenUpdating = True
    
    End Sub
    Firstly, create a new sheet and name it List. Next, starting in cell A1 of the List sheet, list all the sheets that you wish to exclude from the whole process (sheets 11 - 15).

    The code should now only group the sheets that are not listed in the List sheet. You can add or remove sheets from the list as your needs may require in future.

    I hope that this helps.

    Cheerio,
    vcoolio.

  3. #3
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Macro to group / ungroup columns on multiple sheets

    Another option:

    Sub Group_Sheets()
    
    Dim i As Integer
    
    For i = 1 To 10
        Sheets(i).Columns("D:F").Group
        Sheets(i).Columns("O:Q").Group
        Sheets(i).Columns("Z:AB").Group
    Next i
    
    End Sub
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  4. #4
    Registered User
    Join Date
    06-03-2016
    Location
    Malaysia
    MS-Off Ver
    MS365
    Posts
    60

    Re: Macro to group / ungroup columns on multiple sheets

    Thank you so much vcoolio and Arkadi! both work perfectly, you guys help me a lot

  5. #5
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Macro to group / ungroup columns on multiple sheets

    yllew,

    Glad we could help Thanks for the rep, and marking the thread as solved!

  6. #6
    Valued Forum Contributor
    Join Date
    06-29-2014
    Location
    Australia
    MS-Off Ver
    MSO 365
    Posts
    1,111

    Re: Macro to group / ungroup columns on multiple sheets

    Good day Yllew,

    You're welcome. I'm glad that we could help out.
    Thanks for the rep also.

    Cheerio,
    vcoolio.

+ 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. Group Ungroup sheets while protected and shared
    By GabeFlinders in forum Excel General
    Replies: 3
    Last Post: 07-07-2020, 08:40 AM
  2. [SOLVED] Group and ungroup columns using an if then clause
    By Reghu-2013 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-13-2015, 09:40 AM
  3. Group/Ungroup using a Macro
    By HangMan in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-28-2015, 03:15 PM
  4. macro to ungroup/group certain columns and adding to a cell or button
    By sluggerish in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-02-2014, 01:30 PM
  5. Group/ungroup columns in protected sheet
    By bobens_83 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-24-2008, 10:36 AM
  6. Group/Ungroup columns based on Date
    By kingdt in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-20-2007, 12:55 PM
  7. [SOLVED] Group and ungroup protected sheets
    By bob in forum Excel General
    Replies: 1
    Last Post: 02-28-2005, 10:06 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