|
|
|
Referring to Excel Cells by Using a Range Object
|
Referring to Excel
worksheet cells by using
a range object in a macro in Excel and then perform actions on it.
If you set an object variable to a Range object, you can easily
manipulate the Excel range by using the variable name in
Microsoft Excel.
The following procedure creates the object variable myRange and then
assigns the variable to Excel range A1:D5 on Sheet1 in the active Excel
workbook. Subsequent statements modify properties of the Excel range by
substituting the variable name for the range object.
Sub Random()
Dim myRange As Range
Set myRange = Worksheets("Sheet1").Range("A1:D5")
myRange.Formula = "=RAND()"
myRange.Font.Bold = True
End Sub
The training video below shows how to
create
and execute the Excel macro. |
|
|
Implementing the macro:
- Click on Tools in the menu bar, select Macro and from the drop-down list select Visual Basic Editor
- In the Visual Basic editor window click on the menu Insert and select module
- First define the name Random()
- Next use Dim, short for dimension, to set the data type for the variable myRange
- Now define myRange
- The next line generates random numbers in the defined range of cells using a formula
- The second last line formats the random numbers in myRange to bold
- Press F5 or the run button '>' to see the macro in action
|
|
|
|
|