VBA Range Overview : Learn to Use One of the Most Commonly Utilized Worksheet Tools
Have you ever had to manually analyze or update data in Microsoft Excel? When you have a large amount of data that is difficult to manage and maintain, it’s way better to make a VBA program that will automatically manipulate the data according to your intended results. For instance, what if we want to reference a specific column, row or cell address, change cell attribute (font, height or width), or point to a particular cell within the worksheet? Range is a useful tool when dealing with complicated macro program to manipulate large amount of data.
In Microsoft Excel, Range is a group of cells selected in a worksheet. When you selected a number of cells, it will be highlighted by a black outline. By default, there is only cell highlighted when you open a workbook. It can be identified by cell references such as A1:B2 or name given to a selected group of cells.
Below are the most commonly used properties:
- Address – It returns a String value that represents the range of reference.
- AllowEdit – It returns a Boolean value that indicates if the range can be edited on a protected worksheet.
- Cells – It returns a Range object that represents the cells in the selected range.
- Columns – It returns a Range object that represents the columns in the selected range.
- ColumnWidth – It returns or sets the width of all columns in the selected range.
- Count – It returns a Long value that represents the number of objects.
- Font – It returns a Font object that represents the font of the selected object.
- Height – It returns or sets a Variant value that represents the height of the range.
- Offset – It returns a Range object that represents a range that’s offset from the selected range.
- Rows – It returns a Range object that represents the rows in the selected range.
- Row – It returns the number of the first row of the first area in the range.
- ShrinkToFit – It returns or sets a Variant value that indicates if text automatically shrinks to fit in the column width.
- Sort – It dynamically sort any selected columns.
- Text – It returns or sets the text for the selected object.
- Value – It returns or sets a Variant value that represents the value of the specified range.
- Width – It returns a Variant value that represents the width of the range.
- Worksheet – It returns a Worksheet object that represents the worksheet containing the specific range.
expression .Range (Cell1, Cell2)
expression A variable that represents a Range object
|Cell1||Required||Variant||The name of the range. This must be an A1-style reference in the language of the macro. It can include the range operator (a colon), the intersection operator (a space), or the union operator (a comma). It can also include dollar signs, but they?re ignored. You can use a local defined name in any part of the range. If you use a name, the name is assumed to be in the language of the macro.|
|Cell2||Optional||Variant||The cell in the upper-left and lower-right corner of the range. Can be a Range object that contains a single cell, an entire column, or entire row, or it can be a string that names a single cell in the language of the macro.|