Group Rows in Excel

Grouping rows in Excel is a fundamental skill that can significantly enhance your ability to manage and analyze data. Whether you’re working on large datasets or complex financial models, grouping rows helps in organizing your data into a more readable and manageable format. In this detailed blog post, I’ll guide you through the process of grouping rows in Excel, offering tips and tricks along the way to make the most of this powerful feature.

How to Group Rows in Excel: Step by Step Guide

Grouping in Excel allows you to collapse and expand sets of rows or columns, making it easier to navigate through large amounts of data. It’s particularly useful for summarizing data with similar characteristics or for hiding detailed data to focus on overall trends. Follow these steps to group rows in your Excel spreadsheet:

Grouping Rows in Excel Step by Step Tutorial

1. Select the Rows You Want to Group

First, click on the number of the first row you wish to group, then drag down to select all the rows you want to include in the group.

2. Group the Rows

With the rows selected, you can group them in two ways:

  • Using the Ribbon: Go to the Data tab on the Excel ribbon, find the Outline group, and click on the Group button. Then, select Group from the dropdown menu.
  • Right-Click Method: Alternatively, you can right-click on one of the selected rows and choose Group from the context menu.

3. Expand and Collapse the Group

Once grouped, you’ll notice a small box with a minus sign (-) on the left of the grouped rows, indicating that the group is expanded. Clicking on this minus sign will collapse the group, turning the sign into a plus (+). Click the plus sign to expand the group again.

Tips for Effective Grouping

  • Grouping Multiple Levels: Excel allows you to create multiple levels of groups. For instance, after grouping several rows together, you can select a larger set of rows that include the already grouped rows and group again. This creates a hierarchy of groups, which can be expanded or collapsed at various levels.
  • Use the Outline Buttons: On the left of the sheet (or top for columns), Excel displays numbers corresponding to each level of grouping. Clicking on these can expand or collapse all groups at a particular level, offering a quick way to navigate through your data.
  • Ungrouping: To ungroup, select the grouped rows, then either go to the Data tab > Outline group and click on Ungroup, or right-click and select Ungroup from the context menu.
  • Shortcut Keys: For faster grouping and ungrouping, you can use the shortcut keys. To group rows, select the rows and press Alt + Shift + Right Arrow. To ungroup, select the grouped rows and press Alt + Shift + Left Arrow.

Common Uses of Row Grouping in Excel

Row grouping can be used in various scenarios, such as:

  • Financial Modeling: Grouping monthly data to see quarterly or annual summaries.
  • Project Management: Collapsing detailed tasks under larger project phases.
  • Data Analysis: Summarizing data by categories or segments for easier analysis.

Advanced Grouping Techniques

For more advanced users, VBA (Visual Basic for Applications) scripting in Excel can provide greater flexibility in managing groups, including the possibility of naming groups or creating more dynamic group-related functionalities that Excel’s standard UI does not offer. VBA can automate grouping based on specific criteria, manage group states, and even label groups in a way that’s meaningful for your specific context.

However, VBA requires a foundational understanding of programming principles and is best suited for users comfortable with macros and Excel’s developer features.

VBA to Group Rows in Excel

Let’s explore some of the most popular VBA macros for grouping rows in Excel. Each macro serves a different purpose and can be a powerful tool for Excel users looking to automate their data organization tasks.

VBA Macro to Group Rows in Excel

VBA to Group Rows by Value

This VBA macro automatically groups rows based on a specific value in a predetermined column. It’s particularly useful for categorizing data without manually selecting and grouping rows.

VBA Code:

Sub GroupRowsByValue()
Dim rng As Range
Dim cell As Range
Dim lastRow As Long
Dim columnToCheck As Long

columnToCheck = 1 'Change 1 to your column index (A=1, B=2, etc.)
lastRow = Cells(Rows.Count, columnToCheck).End(xlUp).Row
Set rng = Range(Cells(2, columnToCheck), Cells(lastRow, columnToCheck))

Application.ScreenUpdating = False

For Each cell In rng
If cell.Value <> cell.Offset(1, 0).Value Then
Rows(cell.Row & ":" & cell.Offset(1, 0).Row - 1).Group
End If
Next cell

Application.ScreenUpdating = True
End Sub

Uses: This macro is ideal for large datasets where data is sorted, and you want to group rows based on changes in value, such as grouping sales data by month or transactions by customer ID.

VBA to Group and Collapse Rows

This macro groups selected rows and immediately collapses them. It’s handy for quickly organizing sections of your data and hiding details for a cleaner view.

VBA Code:

Sub GroupAndCollapse()
Dim selectedRows As Range
Set selectedRows = Selection.Rows
selectedRows.EntireRow.Hidden = True
End Sub

Uses: Use this macro when you have identified sections of your data that need to be grouped and collapsed for better navigation or when preparing a report that requires a summary view.

VBA to Auto Group Rows by Change

This macro dynamically groups rows every time a change in value is detected in a specified column. It’s useful for datasets where each group’s starting and ending points are defined by changes in data.

VBA Code:

Sub AutoGroupByChange()
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Dim startRow As Long
Dim columnToCheck As Integer

Set ws = ActiveSheet
columnToCheck = 1 ' Change to match the column you're working with
lastRow = ws.Cells(ws.Rows.Count, columnToCheck).End(xlUp).Row
startRow = 2

Application.ScreenUpdating = False

For i = startRow To lastRow
If ws.Cells(i, columnToCheck).Value <> ws.Cells(i + 1, columnToCheck).Value Then
ws.Rows(startRow & ":" & i).Group
startRow = i + 1
End If
Next i

Application.ScreenUpdating = True
End Sub

Uses: Perfect for segmenting continuous datasets into manageable chunks based on a repeating or changing value, such as grouping entries by date, status, or any categorizing attribute.

VBA to Group Rows with Same Value and Add Subtotals

This macro not only groups rows based on identical values in a specific column but also inserts a subtotal for each group. This is particularly useful for financial data or any dataset where sums or averages for groups are needed.

VBA Code:

Sub GroupAndSubtotal()
Dim lastRow As Long, i As Long
Dim columnToCheck As Integer
Dim subtotalColumn As Integer

columnToCheck = 1 ' Column with values to group by
subtotalColumn = 2 ' Column to calculate subtotal

lastRow = Cells(Rows.Count, columnToCheck).End(xlUp).Row

Application.ScreenUpdating = False

For i = lastRow To 2 Step -1
If Cells(i, columnToCheck).Value <> Cells(i - 1, columnToCheck).Value Then
Cells(i, subtotalColumn).Formula = "=SUBTOTAL(9, R[" & 1 - i & "]C:" & "R[-1]C)"
End If
Next i

Application.ScreenUpdating = True
End Sub

Uses: Ideal for reports or analyses where summarizing data by group is necessary, such as sales reports, inventory counts, or expense tracking.

VBA to Group Rows by Product Name

This VBA macro will loop through your dataset, assuming it starts from row 2 (to skip headers) and is located in column B (the “Product Name” column). It will group rows with the same product name together. Before running the script, ensure your data is sorted by the product name if you want contiguous groups.

Sub sbAT_GroupRowsByProductName()
    Dim ws As Worksheet
    Set ws = ActiveSheet ' Or specify a sheet with Set ws = Sheets("YourSheetName")
    Dim lastRow As Long
    lastRow = ws.Cells(ws.Rows.Count, 2).End(xlUp).Row ' Assuming product names are in column B
    Dim startRow As Long
    Dim i As Long
    startRow = 3 ' Start from the first data row
    Application.ScreenUpdating = False
    sbAT_ClearActivSheetGrouping    ' Ungroup any existing groups first
    For i = startRow + 1 To lastRow + 1
        ' Check if the current row's product is different from the previous row's product
        If ws.Cells(i, 1).Value <> ws.Cells(i - 1, 1).Value Then
            ' Group from the startRow to the row before the current row
            If i - startRow > 1 Then ' Ensure there are at least two rows to group
                startRow = startRow
                ws.Rows(startRow & ":" & (i - 1)).Group
            End If
            startRow = i + 1 ' Reset the startRow to the current row
        End If
    Next i
    Application.ScreenUpdating = True
End Sub

Sub sbAT_ClearActivSheetGrouping()
On Error GoTo errOccured
Dim selectedRows As Range
ActiveSheet.Rows.Hidden = False
Dim lastRow As Long
lastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, 2).End(xlUp).Row
ActiveSheet.Rows(1 & ":" & lastRow).Ungroup
End Sub

How to Use This Macro

  1. Prepare Your Data: Ensure your data is in the correct format, with product names in column B and starting from row 2.
  2. Sort Your Data: For effective grouping, sort your dataset by the product name column (Column B in this case).
  3. Open VBA Editor: Press Alt + F11 to open the VBA Editor in Excel.
  4. Insert the Macro: In the VBA Editor, right-click on your workbook name in the “Project” window, go to Insert > Module, and paste the provided code into the module window.
  5. Run the Macro: Press F5 while in the module window or close the VBA Editor and use the Macro dialog in Excel (View > Macros > View Macros > Select 'GroupRowsByProductName' > Run).

Use Case

This macro is particularly useful for sales data analysis, allowing users to quickly organize data by product name. Once grouped, you can easily collapse or expand each product’s sales data to analyze trends, compare sales figures, or prepare summarized reports. This automated grouping saves significant time, especially with large datasets, and ensures that data is consistently organized for analysis or reporting purposes.

Download the Example File

To help you get the most out of this tutorial on grouping rows in Excel, we’ve prepared an example Excel file that corresponds to the scenarios and VBA code examples discussed in this post. This file contains a pre-populated dataset of 50 rows, featuring sales data that you can use to practice the various grouping techniques and VBA macros provided.

Grouping Rows in Excel

How to Use the Example File

  1. Download and Open: Click the link above to download the Excel file. Once downloaded, open the file in Excel to get started.
  2. Explore the Data: Familiarize yourself with the dataset provided in the file. Notice how the data is structured, with sales information spread across different rows, categorized by date, product name, and sales amount.
  3. Apply the VBA Scripts: Follow the instructions in our blog post to access the VBA Editor and insert the provided VBA scripts. These scripts are designed to automatically group rows based on specific criteria, such as product name.
  4. Experiment with Grouping: Use the Excel file to manually group and ungroup rows, as well as to run the VBA scripts. This hands-on practice will deepen your understanding of how row grouping works in Excel and how it can be automated through VBA.
  5. Modify and Test: Feel free to modify the dataset or the VBA scripts to test different scenarios. This experimentation is a great way to learn more about the capabilities and limitations of grouping rows in Excel.

Feedback and Questions

We love to hear from our readers! If you have any feedback about the example file, questions about the techniques covered, or suggestions for future tutorials, please leave a comment below or contact us directly. Your input helps us improve and expand our content to better serve your learning needs.


How to Group Rows to Collapse in Excel

To group rows in Excel for collapsing, select the rows you want to group, then go to the Data tab and click on Group under the Outline group. This action will allow you to collapse the grouped rows using the “-” sign that appears at the beginning of the group.

Shortcut for Grouping Rows in Excel

The keyboard shortcut for grouping rows in Excel is Alt + Shift + Right Arrow. To ungroup, use Alt + Shift + Left Arrow.

Grouping Consecutive Rows in Excel

Select the consecutive rows you wish to group, then either use the Data tab method or the right-click context menu to select Group.

Combining Rows in Excel

Combining rows typically involves concatenating text values or aggregating numerical data across multiple rows into a single row. Use formulas like CONCATENATE, TEXTJOIN (for text values), or SUM, AVERAGE (for numerical values) across the rows you wish to combine.

Automatically Grouping Rows

To automatically group rows based on criteria or values, you might need to use a combination of Excel’s sorting features and the Subtotal function available under the Data tab, which can automatically group and summarize data based on the column you choose.

Grouping Rows in Google Sheets

In Google Sheets, select the rows you wish to group, right-click, and choose Group rows X-Y from the context menu. Google Sheets also supports collapsing and expanding groups similarly to Excel.

Grouping Rows in Excel by Cell Value

To group rows based on cell values, you’ll first need to sort your data by the column containing the values you wish to group by. Then, you can manually group rows or use the Subtotal feature under the Data tab to create groups based on the sorted column.

Shortcut for Group Row

The shortcut for grouping rows is Alt + Shift + Right Arrow, and to ungroup is Alt + Shift + Left Arrow. This shortcut also applies to grouping columns, with the direction of the arrow key indicating the action.

Excel Grouping Mechanics

Excel grouping works by creating a visual and functional cluster of rows or columns, which can be easily expanded or collapsed. This does not change the actual data but rather affects how it’s displayed and navigated.

Shortcut to Group Rows and Columns

The shortcut to group rows or columns in Excel is the same: Alt + Shift + Right Arrow for grouping and Alt + Shift + Left Arrow for ungrouping.

Grouping Sheets in Excel

To group sheets in Excel, hold down the Ctrl key and click on each sheet tab you want to group. You can then perform operations across all grouped sheets simultaneously.

Grouping Rows in Excel with Headers

To ensure headers remain visible when grouping rows, do not include the header row in your grouping selection. Group the rows beneath the header so you can collapse and expand the group while keeping the header visible.

Grouping and Moving in Excel

Group rows or columns as needed, then use the Excel outline symbols to collapse the group if desired before moving it. Note that moving grouped rows/columns should be done carefully to maintain data integrity.

Shortcut to Group Columns in Excel

The shortcut for grouping columns in Excel is Alt + Shift + Right Arrow, similar to grouping rows.

Grouping and Subtotal Rows in Excel

Use the Subtotal function under the Data tab after selecting the rows you wish to group. This feature allows you to automatically group rows and calculate subtotals based on a specific column.

Sorting Groups in Excel

To sort groups in Excel, it’s best to collapse the groups first to sort by the summary rows. Select the range you want to sort, then use the Sort function under the Data tab.

Grouping Duplicate Rows

To group duplicate rows, first, identify duplicates using conditional formatting or a formula. Then, you can manually group these rows together.

Grouping in Spreadsheets

Grouping in spreadsheets refers to the practice of creating a collapsible and expandable set of rows or columns, aiding in data management and analysis.

Creating a Group Structure in Excel

Create a hierarchical group structure by first grouping a set of rows or columns, then selecting a broader range including the initial group to create a second-level group, and so on.

Filtering Groups in Excel

Use the Excel filter function on grouped data by applying the filter to the header row. Groups can be expanded or collapsed while filters are applied, allowing for dynamic data analysis.

Grouping Excel Tabs

While you cannot create a collapsible group of tabs in Excel, you can group sheets by selecting multiple tabs (using Ctrl click) to perform batch operations.

Naming Groups in Excel

Excel does not allow


Mastering the grouping feature in Excel can significantly improve your data management and analysis capabilities. By following the steps and tips outlined above, you can efficiently organize your data, making it more accessible and interpretable. Whether you’re a beginner or an experienced Excel user, leveraging the power of row grouping will undoubtedly enhance your spreadsheet management skills.

Share This Story, Choose Your Platform!

Leave A Comment