How to Access Excel Worksheet Cells with a Macro Using the Range Property
Before you can start performing calculations with your Excel data, you obviously have to enter data into the worksheet. Now if you wish to enter data with the help of a macro you need to know how to access a worksheet cell like A1 or a range of cells like A1:C1. The ‘range’ property helps you to access a single cell or a range of cells. Later you can define whether a cell can contain text, number, currency, formulas or functions.
- Click on the ‘Developer’ Tab
- From the ‘Code’ group select Visual Basic
- The Microsoft Visual Basic window pops up
- Click on ‘Insert’ and select ‘Module’.
- In the white workspace area type ‘Sub macroname()’. Select a macro-name that is short and easy to remember and describes well what the macro will do
- Press enter and you will notice that the application automatically adds ‘End Sub’
- Between the ‘Sub macroname ()’ and the ‘End Sub’ you will write the code that will execute what the macro is supposed to do
We enter the following code for example:
Range(“A3”) = “Name”
Range(“B3”) = “Salary”
When we click on the run arrow-like button (>) the macro executes the code and after minimizing the MS-Visual Basic Window we can observe that the text ‘Name’ and ‘Salary’ have been placed in the appropriate worksheet cells ‘A1’ and ‘B1’.
Let’s have a look at the MS Excel VBA training video.