Select all cells in Excel using VBA

In this post, we shall see how to select all cells in a worksheet using VBA. We shall take help of the Range object. The Worksheet object in excel has Range as one of its properties. The Range property can be used to select a cell, group of cells or range in excel. The following code will select all the cells that contain data, starting from cell A1.

As mentioned in the office VBA reference, the range object can be used on a cell, row, column as well as a selection of cells (containing one or more contiguous blocks). 
Here, we are using the Range.Select method, which has the following syntax:

In the SelectAll procedure above, we first select the cell A1 using the Range.Select method. Now that we have selected cell A1, we are extending our selection from cell A1 to the last cell in the row that contains data. To do this, we use the Range.End property. The syntax of this property is as follows:

We first extend the selection towards the right (line 3) and then extend it down (line 4). This completes the procedure to select all cells with data in excel.
For example, executing this code on the table shown in the picture will produce the following result.



References: Microsoft Office VBA Reference
1. https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-object-excel
2. https://msdn.microsoft.com/en-us/vba/word-vba/articles/range-select-method-word
3. https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-end-property-excel