When creating a class in VBA and dealing with properties (member variables), there are three Property methods: Get, Set, and Let.
In languages other than VBA, it is common to use only Getter and Setter to set properties (member variables).
Therefore, some people may feel uncomfortable when they first see the definition of properties in VBA.
This time, let’s summarize the difference between Property Set and Property Let in VBA.
Difference between Property Set and Property Let
In conclusion,
- Set is required when assigning a value to a reference type variable.
- Let is used for other cases.
I believe this understanding is sufficient.
Explanation
When assigning a value to a reference type variable, Set must be used, otherwise it will result in an error.
For example, in the case of a worksheet:
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets(1)
This applies to properties in classes as well. When you want to set a value to a property in a class,
' Class1 Module
Private m_ws As Worksheet
' Since the Worksheet class is a reference type, it needs to be defined with Set
Public Property Set ws(ByRef received As Worksheet)
' Set is also required when assigning a value to the property
Set m_ws = received
End Property
' Standard Module
Dim c1 As Class1
Set c1 = New Class1
' Set is also required when calling it
Set c1.ws = ThisWorkbook.Worksheets(1)
It will result in an error if there is no Set for at least one.
It’s quite cumbersome, but it is safe to understand that the concept of Set in variable declaration is also applied to Property in VBA.
Execution Test
' Class1 Module
Private m_ws As Worksheet
Public Property Set ws(ByVal received As Worksheet)
Set m_ws = received
End Property
Public Property Get ws() As Worksheet
Set ws = m_ws
End Property
' Standard Module
Public Sub test()
Dim c1 As Class1
Set c1 = New Class1
Set c1.ws = ThisWorkbook.Worksheets(1)
Debug.Print c1.ws.Cells(1, 1).Value
End Sub
The value of the first cell of the first sheet of the workbook will be displayed in the Immediate window.
Use Property Let for value-type properties
Properties that hold value types such as strings and numbers are defined with Property Let, and the caller assigns to them without Set.
' Class1 Module
Private m_name As String
Public Property Let Name(ByVal received As String)
m_name = received
End Property
Public Property Get Name() As String
Name = m_name
End Property
' Standard Module
Dim c1 As Class1
Set c1 = New Class1
c1.Name = "Sato" ' a value type, so Set is not needed
Don’t use Let as a substitute for Set
For properties that receive an object reference, Property Set is the standard choice. Microsoft’s VBA reference also describes Property Set as the procedure that sets a reference to an object, and Property Let as the procedure that assigns a value.
Apart from special designs such as Variant properties that may hold either a value or an object (in which case you provide both Property Let and Property Set), the following split keeps your intent clear to readers.
| Kind of property | Procedure to use | Example |
|---|---|---|
| String, number, date, Boolean | Property Let | Name As String, Amount As Long |
Object reference such as Worksheet or Range | Property Set | TargetSheet As Worksheet |
Having to write Set c1.ws = ... may look tedious, but it makes it explicit that an object reference is being assigned. For readability and maintainability, it is safer to choose Let or Set according to the type.
Related article
For practical uses of Property procedures and a detailed explanation of class modules, see the article below.
The page you are looking for is currently under preparation. Please wait a moment.
In Conclusion
Some may feel that VBA is outdated, but there are still ways to utilize Excel in the SaaS era.
One of those ways is by using APIs of cloud services from VBA.
We have introduced some of these methods on our website, so please take a look if you’re interested.