Making ranges work
In this last recipe, we're going to create and add data to a short list, making use of everything we've learned in the sections about references, properties, and methods.
Getting ready
Ranges.xlsm
needs to be kept open. Now, follow these steps:
- Insert a new sheet, and then add the following data to Sheet2:
- Press Alt + F11 to activate the VBA Editor, and then insert a new module and change its name to
Range_Select
.
How to do it…
- In the Editor, create a new Sub procedure:
Sub RangeSelect() End Sub
- Add the following line of code to select cell A5:
Sub RangeSelect() Range("A5").Select End Sub
- Next, change the value of cell A5 with the following code:
Sub RangeSelect() Range("A5").Select ActiveCell.Value = 4 End Sub
- Add another line of code to assign a value to cell B5. Instead of the normal referencing, we...