Share via


How to: Create Groups in Worksheets

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

  • Application-level projects

Microsoft Office version

  • Excel 2003

  • Excel 2007

For more information, see Features Available by Application and Project Type.

You can group one or more whole rows. To create a group in a worksheet, use a NamedRange control or a native Excel range object.

Using a NamedRange Control

If you add a NamedRange control to a document-level project at design time, you can use the control to programmatically create a group. The following example assumes that there are three NamedRange controls on the same worksheet: data2001, data2002, and dataAll. Each named range refers to a whole row in the worksheet.

To create a group of NamedRange controls on a worksheet

  • Group three named ranges by calling the Group method of each range. This code must be placed in a sheet class, not in the ThisWorkbook class.

    With Me
        .data2001.Group()
        .data2002.Group()
        .dataAll.Group()
    End With
    
    this.data2001.Group(missing, missing, missing, missing);
    this.data2002.Group(missing, missing, missing, missing);
    this.dataAll.Group(missing, missing, missing, missing);
    

Using Native Excel Ranges

The code assumes that you have three Excel ranges named data2001, data2002, and dataAll on a worksheet.

To create a group of Excel ranges in a worksheet

  • Group three named ranges by calling the Group(Object, Object, Object, Object) method of each range. The following example assumes that there are three Range controls named data2001, data2002, and dataAll on the same worksheet. Each named range refers to a whole row in the worksheet.

    With Me.Application
        .Range("data2001").Group()
        .Range("data2002").Group()
        .Range("dataAll").Group()
    End With
    
    this.Application.get_Range("data2001", missing)
        .Group(missing, missing, missing, missing);
    
    this.Application.get_Range("data2002", missing)
        .Group(missing, missing, missing, missing);
    
    this.Application.get_Range("dataAll", missing)
        .Group(missing, missing, missing, missing);
    

See Also

Tasks

How to: Clear Groups in Worksheets

How to: Add NamedRange Controls to Worksheets

Concepts

Working with Worksheets

NamedRange Control

The Variable missing and Optional Parameters in Office Solutions