VBA Cells Range Methods and Property
In VBA, Cells and Range are two important objects that are used to manipulate data in Excel worksheets.
Cells object is used to refer to a single cell in a worksheet. It is usually used when you need to perform an operation on a single cell. For example, the following code sets the value of cell A1 to 1990:
Cells (1, 1). Value = 1990
Range object is used to refer to a group of cells in a worksheet. It is usually used when you need to perform an operation on a group of cells. For example, the following code sets the values of cells A1 to A10 to 20:
Range (“A1:A10”). Value = 20
You can also use the Range object to refer to a single cell. For example, the following code sets the value of cell A1 to 1990:
Range(“A1”). Value = 1990
VBA Select and Activate Methods
The VBA select and activate methods are used to manipulate objects in Excel, such as sheets, ranges, cells, shapes, charts, etc. Here are some differences and similarities between them:
- Select can be used to select one or more objects at the same time, while activate can only select one object within a selection or by itself.
- Select works only on the active worksheet, so you need to activate or select the worksheet before using select on a range of cells. Activate can be used to select workbooks, which do not have a select method.
- Select and activate can perform the same action when there is only one object to select, but activate is more efficient and avoids unnecessary focus shifts that can affect performance and stability.
For example,
If we want to select cell D5 use below command
Range(“D5”). select
We can also select multiple range like to select range E1 to E5 use below code
Range (“E1:E5”). select