Excel VBA Tip – Expand / Collapse all the Groups in Excel

.

Dear Readers,

This is a small VBA tip to automate to expand or collapse groups created in Excel. Before I jump to VBA code about Expanding or collapsing the groups in excel, I would like to brief you about Grouping or Outlining Functionality in Excel.
It is always good to know “How to do it manually. before automating any step.

What is Grouping in Excel?

Most of you who use excel in their day-to-day work, know about grouping. I will explain you about grouping feature of Excel for those who does not know about this. As the name itself suggests, this is the inbuilt feature in Excel which allows users to group their Rows or Columns accordingly. Grouping can be done at Rows and Columns level both. This is called as Outlining as well.

Steps to do Grouping/Outlining in Excel?

Step 1.

Select your Rows or Columns where you want to apply the Grouping (Outlining).

Step 2.

Go to “Data” Tab in Excel Ribbon and Click on Group Button as shown in the below image:

Outline-Excel-Ribbon

 

When certain Rows or Columns are grouped then user will be able to see a + (Plus) sign to expand all the rows or columns grouped together. Once all the rows or columns are expanded then user will be able to see a – (Minus) sign to collapse them as shown in the below picture.
 
Different - Outline - Levels
 

Is it possible to have nested Grouping?

Now what is nested grouping? As Nested means Nested grouping is Grouping within a Group. I will explain this by giving an example. Suppose you have grouped Rows 5 to 20. Within this group, create another sub-group of rows 8 to 12. Same case is with Column grouping as well. Each nesting is called as Level. Sub-grouping can be done up to 8 levels in Microsoft Excel. Refer the below picture. There are maximum of 8 Levels present. You will not be allowed to group any further level.
 
Outlining Row and Column
 

VBA Method used to Display Outline Levels

Outline.ShowLevels is a method which is used to show outlines of different levels. This method takes RowLevel and ColumnLevel as input.

.Outline.ShowLevels(RowLevels, ColumnLevels)

Where:

RowLevels:

This is the Row Level number UP TO which you want to show. It means if you pass this number as 7 then this method will show all the levels till 7. It will not expand the 8th Level but all the levels before 7.

ColumnLevels

This is the Column Level number UP TO which you want to show. It means if you pass this number as 7 then this method will show all the Column levels till 7. It will not expand the 8th Level but all the levels before 7.

VBA Code to Expand all Outline Levels

As mentioned earlier there can be a maximum of 8th Level in Row and Column grouping. Therefore to expand all the levels available in a sheet you can pass the maximum possible level number i.e. 8 for both Row and Column Level numbers.


Sub Expand_All()
    ActiveSheet.Outline.ShowLevels RowLevels:=8, ColumnLevels:=8
End Sub

VBA Code to Collapse all Outline Levels

There is only one method .ShowLevels is available. There is no method for hiding all the levels. But if you understand clearly the meaning of parameters passed as RowLevels and ColumnLevels then you will be able to achieve this as well using the same method.
 
As mentioned earlier LevelNumber are the level number to display all the levels UP TO that level ONLY. It means all the levels beyond that will not be shown. Hence if I pass Row and Column Parameter as 1 then only first Level will be displayed and all other levels will be hidden and that is how we achieved both the goals (Expand All & Collapse All) with the same method.


Sub Collapse_All()
    ActiveSheet.Outline.ShowLevels RowLevels:=1, ColumnLevels:=1
End Sub

If you want any specific level to be shown in Row or Column levels, you can achieve it by changing the parameters value.

Buy a coffee for the author

Adsense

Download FREE Tools and Templates

There are many cool and useful excel tools and templates available to download for free. For most of the tools, you get the entire VBA code base too which you can look into it, play around it, and customize according to your need.

Convert PDF Tables to Excel with VBA | Step-by-Step Guide
Convert PDF Tables to Excel with VBA | Step-by-Step Guide

Convert PDF Tables to Excel with VBA | Step-by-Step Guide How to Convert PDF Tables to Excel Using Excel VBA (Macros) Extracting data from PDF tables into Excel can be tedious, but with Excel VBA (macros), you can automate this process and save time. This guide will...

Dynamic Arrays and Spill Functions in Excel: A Beginner’s Guide
Dynamic Arrays and Spill Functions in Excel: A Beginner’s Guide

In today's tutorial, we'll be diving into the exciting world of dynamic arrays and spill functions in Office 365 Excel. These features have revolutionized the way we work with data, providing a more flexible and efficient way to handle arrays. I am going to explain...

How to Declare a Public Variable in VBA
How to Declare a Public Variable in VBA

While programming in VBA sometimes you need to declare a Public Variable that can store the value throughout the program. Use of Public Variable: Let's say you have 4 different Functions in your VBA Code or Module and you have a variable that may or may not be...

What is Excel Formula?

Excel Formula is one of the best feature in Microsoft Excel, which makes Excel a very very rich application. There are so many useful built-in formulas available in Excel, which makes our work easier in Excel. For all the automated work, Excel Macro is not required. There are so many automated things can be done by using simple formulas in Excel. Formulas are simple text (With a Syntax) which is entered in to the Excel Worksheet Cells. So how computer will recognize whether it is a formula or simple text? Answer is simple.. every formula in Excel starts with Equal Sign (=).

You May Also Like…

7 Comments

  1. John Peters

    How do I adjust this code to make it apply for all the worksheets and not only one?

    Reply
    • Brad

      Private Sub xyz()
      Dim WS_Count As Long
      Dim i As Long

      WS_Count = ActiveWorkbook.Worksheets.Count

      For i = 1 To WS_Count

      ActiveWorkbook.Worksheets(i).Activate
      ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=1
      ActiveSheet.Outline.ShowLevels RowLevels:=1
      Next

      End Sub

      Reply
  2. Ton Walter

    Thanks for the detailed, very useful, references.

    When I enter the code in my macro to collapse or expand the groups, my Excel hangs, and I must terminate the application via the task manager. As it closes, I am able to briefly see an Excel window with a message pointing to an automation error.

    Would you happen to have any hints as to why I might be having trouble running this code?

    Thanks and best regards.

    Reply
  3. Gusto

    Step 1
    Use a separate VBA Code to group some columns (eg Columns T-V)

    The VBA Code below basically highlights the columns (T-V) I want hidden and then this code hides them, with the Group/Ungroup function in tact.

    ‘ hide columns
    Columns(“T:V”).Select
    Selection.EntireColumn.Hidden = True
    Range(“A9”).Select

    Reply
  4. Paulo

    Hi,
    I did a code below:

    Columns(“B:E”).Select
    Selection.Columns.Group
    Columns(“G:M”).Select
    Selection.Columns.Group
    Columns(“R:S”).Select
    Selection.Columns.Group
    Columns(“W:AG”).Select
    Selection.Columns.Group
    ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=1

    But the group isn’t colapse and I need click in control to colapse the columns.

    What I doing wrong?

    Sorry about my english.

    Thanks!

    Reply
  5. Tyra mills

    encontrar amigos policia aérea portuguesa

    Reply
  6. Ezequiel Pardo

    Hi, is there any workaround for controlling individual Outlines, not all at the same time?
    Thanks

    Reply

Trackbacks/Pingbacks

  1. Hide Group outline symbol in Excel - [Manual Option + VBA] - Welcome to LearnExcelMacro.com - […] Refer the highlighted part in the below image. That is called group outline which is, by default, becomes visible…

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Join and get a FREE! e-Book

Don't miss any articles, tools, tips and tricks, I publish here

You have Successfully Subscribed!

Pin It on Pinterest