Properties
These are the quantifiable characteristics of an object.
You can find out whether an exception will be generated by looking in the object model documentation.
Property values can be Numerical, String or Boolean and they may be specific or apply to several objects.
They can be obtained or set by assignment statements.
You can easily use the NOT operator to toggle properties on or off. (eg ActiveWindow.DisplayAlerts = NOT ActiveWindow.DisplayAlerts).
Declarations
Also known as property procedures.
Property Get - used to retrieve a property of a class. Get the value of scalar of object property.
Lets you return a property value or property reference
Take action before retrieving
Return a calculated value
Property Let - used to assign the value to a property of a class. Set the value of scalar property.
Lets you define a new property value
Take action after setting
Perform data validation
Property Set - used to assign an object to a property of a class. Set a reference for object property.
Lets you define a new property reference
Property - Collection
Private field_colValue As Collection
Property Get Property_Name() As Collection
Set Property_Name = field_colValue
End Property
' You would use Set because Collection is a reference data type
Property Set Property_Name(colData As Collection)
Set field_colValue = colData
End Property
Property - Object
Private field_oValue As Object
Property Get Property_Name() As Object
Set Property_Name = field_oValue
End Property
' You would use Set because Object is a reference data type
Property Set Property_Name(oData As Object)
Set field_oValue = oData
End Property
Property - Date
Private field_dtValue As Date
Property Get Property_Name() As Date
Property_Name = field_dtValue
End Property
' You would use Let because Date is a value data type
Property Let Property_Name(dtData As Date)
field_dtValue = dtData
End Property
Property - String
Private field_sValue As String
Property Get PropertyName() As String
PropertyName = field_sValue
End Property
' You would use Let because String is a value data type
Property Let PropertyName(sData As String)
field_sValue = sData
End Property
Property - Variant
Private field_vValue As Variant
Property Get Property_Name() As Variant
If IsObject(field_vValue) Then
Set Property_Name = field_vValue
Else
Property_Name = field_vValue
End If
End Property
' You could use either Let or Set
Property Let Property_Name(vData As Variant)
field_vValue = rData
End Property
Property Set Property_Name(vData As Variant)
Set field_vValue = vData
End Property
Remember
If you are using an Object data type then you must use Set instead of Let.
Public Variables
Public variables declare the properties for the object.
When your properties are defined Public they are automatically available as properties of the class.
Any variables declared inside a class (even those declared as public) are not available outside of the class.
This is particularly relevant to Worksheet and Userform class modules.
Public property_sName As String
Public property_dbHourlyRate As Double
Both these variables are declared as Public and therefore automatically become properties of this class.
Disadvantages of Public Variables
Class has no way of knowing if an outside process has changed the value.
There is no way of performing any data validation.
There is no way of creating read-only or write-only properties.
Alternative to Public Variables
The alternative is to use fields with property procedures instead.
Property Get - used to retrieve a property of a class. Get the value of scalar of object property.
Property Let - used to assign the value to a property of a class. Set the value of scalar property.
Property Set - used to assign an object to a property of a class. Set a reference for object property.
Property Get
Used to retrieve a property of a class.
This is exactly the same as just using a private variable.
Public Property Get Property_Department() As String
Property_Department = field_sDepartment
End Property
The following line gets the department by calling the property Get procedure
Call MsgBox(oEmployee.Property_Department)
Public Property Get Property_HoursPerWeek() As Double
Property_HoursPerWeek = field_dbNormalHours + field_dbOvertimeHours
End Property
Returns a property object reference
Public Property Get Property_SomeThing() As Object
Set Property_SomeThing = field_oObject
End Property
Property Let
used to change the (or assign) the value of a class property.
This statement lets you define a procedure that assigns a value to a property.
If you want to have full control when a property is initialised you can define the property using a Property Let procedure.
Public Property Let Property_One()
End Property
Passing Parameters
It is possible to pass parameters although not best practice.
Department
Public Property Let Property_Department(sDepartment As String)
If (sDepartment <> "Sales") And (sDepartment <> "Research") Then
Err.Raise "This is not a valid department name"
Exit Property
End If
field_sDepartment = sDepartment
End Property
The following line assigns a department by calling the Property Let procedure
objEmployee.Property_Department = "Sales"
Hours Per Week
Public Property Let Property_HoursPerWeek(dbHoursPerWeek As Double)
field_dbNormalHours = WorksheetFunction.Min(35, field_dbHoursPerWeek)
field_dbOvertimeHours = WorksheetFunction.Max(0, field_dbHoursPerWeek - 35)
End Property
Write Only Properties
These are not used very often but it is possible to create jus a write-only property
Just provide a property let procedure (no property set)
Property Set
Used to assign (or change) an object assigned to a property.
Creates a reference to an object
This can be used to set a property to an object.
You can't define a property set inside another function or subroutine.
Private field_oEmployee As Object
Public Property Set Property_InitialiseObject(oObject As Object)
Set field_oEmployee = oObject
End Property
You can only use a Property Set procedure on the left side of an object reference
objEmployee.Property_InitialiseObject = oEmployee
Read Only Properties
It is very common to have read-only properties but not write-only.
A large number of the properties are typically read-only.
This property only has a Property Get (no Property Let) which means that it is read-only.
Public Property Get Property_NormalHours() As Double
Property_NormalHours = field_dbNormalHours
End Property
This property only has a Property Get (no Property Let) which means that it is read-only.
Public Property Get Property_OvertimeHours() As Double
Property_OvertimeHours = field_dbOvertimeHours
End Property
A lot of the object model classes contain a lot of properties that cannot be set.
If a property cannot be set, it is read-only as opposed to read-write).
Many properties have built in constants that should be used for their values.
Using a Function Instead
You can create functions that can be treated like a read-only property
Write Only Properties
When the value of a property can only be changed outside the class it is called a write-only property.
This is rarely seen or used.
There may also be times when you want the client to change a value but not to retrieve it.
This rarely makes sense though.
When a write-only property is required a method is often used instead.
Exit Property
This can be used to cause an immediate exit from the property
You can use any number in a property statement.
Private pAge As Integer
Public Property Let Age(ByVal NewAge As Integer)
If (NewAge < 0) Then
MsgBox "Error: Age cannot be negative!", vbCritical
Exit Property
End If
pAge = NewAge
End Property
Public Property Get Age() As Integer
Age = pAge
End Property
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext