Set is a Keyword in VBA programming used to add a reference to an object or cell range, which remains fixed in the program in Excel. VBA Set is used to avoid repetition of inputs in Excel.
How To Use Excel VBA Set
You can use the following way to use VBA Set in Excel.
Step 1. Click on the Visual Basic option in the Developers tab.
Step 2. Double-click Sheet1(Sheet1) to write the code. Then, declare a subset Sub and press enter.
Code:
Sub setexmp()
End Sub
Step 3. Now, Declare the variables, which we need in the code.
Code:
Sub setexmp()
Dim Rnst As Range
End Sub
Step 4. Then type the Set keyword and give a range using Set variable_name = Range(“cell range”).
Code:
Sub setexmp()
Dim Rnst As Range
Set Rnst = Range("A2:A11")
End Sub
Step 5. We have to use a specified row range. Then, type Rnst.Select.
Code:
Sub setexmp()
Dim Rnst As Range
Set Rnst = Range("A2:A11")
Rnst.Select
End Sub
Step 6. Run the code by using the F5 or the Run button to see the output.