VBA proper use of set

TopLearner

Board Regular
Joined
Feb 25, 2022
Messages
57
Office Version
  1. 365
Platform
  1. Windows
Good afternoon there,



I am pretty new to VBA and I am struggling to understand some certain concepts even I have been watching a few videos.



According to the definition of set: “Set”, is a keyword used in VBA programming to assign a reference to an object or cell range which is going to remain fix throughout the program or code in Excel. VBA Set basically helps us in avoiding repetitive input of range we need to select while executing the code. In excel, “Set” is mostly applied on worksheets/cell range in a particular sheet”.



For instance,



VBA Code:
Sub SetRnge


Dim Rnge As Range

Set Rnge = Range (“A2:A11”)

Rnge.Select

End Sub



However, I do not understand why other times we do not have to use “set”



VBA Code:
Sub AddMultipleSheet2()



Dim sheets_count As Integer

Dim sheet_name As String

Dim i As Integer


sheet_count = Range("A1:A7").Rows.Count


For i = 1 To sheet_count


sheet_name = Sheets("mySheet").Range("A1:A10").Cells(i, 1).Value


If SheetCheck(sheet_name) = False And sheet_name <> "" Then

Worksheets.Add().Name = sheet_name

End If



Next i


End Sub




Why in this case I do not write?



Set sheet_count = Range("A1:A7").Rows.Count





From my first Sub ( SetRnge), could I also write:



Set Rnge = Range (“A2:A11”). Select?



Thanks

Sanchez
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
I think that part of your confusion might be that
VBA Code:
Range("A1:A10").Rows.Count
references a work sheet, but since the .Count returns a numeric value, that is what triggers the need (or not need in this case) of Set.
 
Upvote 0
There are lots of ways to do everything in Excel.

I hardly ever use SET

Here are several ways do this small script.
Now which way is faster I do not know.

VBA Code:
Sub Using_Set()
'Modified 10/31/2022  11:34:01 AM  EDT
Dim sn As Worksheet
Set sn = Worksheets("Apple")
sn.Range("A1").Value = "Alpha"
sn.Range("A2").Value = "Bravo"


With Sheets("Apple")
    .[B1].Value = "Charlie"
    .Range("B2").Value = "Delta"
End With

End Sub
 
Upvote 0
I think that part of your confusion might be that
VBA Code:
Range("A1:A10").Rows.Count
references a work sheet, but since the .Count returns a numeric value, that is what triggers the need (or not need in this case) of Set.
Hi Mikerickson,

Thanks for your help.


Does it mean that only when I want to assign an object to a variable I have to use set, and when I am expecting a value I do not have to use set?

Thanks
Sanchez
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,252
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top