Free Microsoft Excel Training Videos

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:
  1. Click on Tools in the menu bar, select Macro and from the drop-down list select Visual Basic Editor
  2. In the Visual Basic editor window click on the menu Insert and select module
  3. First define the name Random()
  4. Next use Dim, short for dimension, to set the data type for the variable myRange
  5. Now define myRange
  6. The next line generates random numbers in the defined range of cells using a formula
  7. The second last line formats the random numbers in  myRange to bold
  8. Press F5 or the run button '>' to see the macro in action
More resources on this Excel topic
Referring to ranges directly

Referring to Excel worksheet cells by using a range object