How To Use ‘Set’ In VBA – Complete Guide

How To Use 'Set' In VBA

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.

Visual Basic in Developer Menu

Step 2. Double-click Sheet1(Sheet1) to write the code. Then, declare a subset Sub and press enter.

Code:

Sub setexmp()

End Sub
Creating SubSets

Step 3. Now, Declare the variables, which we need in the code.

Code:

Sub setexmp()

Dim Rnst As Range

End Sub
Declaring variables

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
Creating vba sets

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
select as Rnst.Select

Step 6. Run the code by using the F5 or the Run button to see the output.

Output of VBA Set

Read More: The Application-Specific Permission Settings Do not Grant Local Activation Permission For The COM Server Application With CLSID

Leave a Comment

Your email address will not be published. Required fields are marked *