How to Select All Cells with Data in VBA

In VBA, to select all cells with data, use the Range.SpecialCells method and specify the xlCellTypeVisible constant as the argument. This will select the visible cells in the active worksheet that have data in them. For example, to select all visible cells with data, use: Range(“A1:C30”).SpecialCells(xlCellTypeVisible).Select. This will select all of the visible cells in the range A1:C30 that contain data.


You can use the following methods in VBA to select all cells with data in a sheet:

Method 1: Select Grid of Cells with Data

Sub SelectCellsWithData()

Range("A1").CurrentRegion.Select

End Sub

This particular macro will select a grid of cells with data starting from cell A1 of the currently active sheet.

Method 2: Select Individual Cells with Data

Sub SelectCellsWithData()

Worksheets("Sheet1").Activate
ActiveSheet.Cells.SpecialCells(xlCellTypeConstants).Activate

End Sub

This particular macro will select all of the individual cells with data from Sheet1.

The following examples show how to use each method in practice with the following sheet called Sheet1 in Excel:

Example 1: Select Grid of Cells with Data

Suppose we would like to select the grid of cells that contains data in Sheet1.

We can create the following macro to do so:

Sub SelectCellsWithData()

Range("A1").CurrentRegion.Select

End Sub

When we run this macro, we can see that the entire grid of cells that contains data in Sheet1 has been selected:

Note that this method uses the CurrentRegion property to select the range of cells bounded by empty cells.

You can find the complete documentation for the CurrentRegion property .

Example 2: Select Individual Cells with Data

Suppose we would like to only select all of the individual cells that contain data in Sheet1.

We can create the following macro to do so:

Sub SelectCellsWithData()

Worksheets("Sheet1").Activate
ActiveSheet.Cells.SpecialCells(xlCellTypeConstants).Activate

End Sub

When we run this macro, we can see that each of the individual cells in Sheet1 that contain data are selected:

x