REAL-TIME

VBA Projects

Full Access with Source Code

  • Designed and Developed by PNRao

  • Full Access with VBA Source Code

  • Well Commented Codes Lines

  • Creative and Professional Design

120+ PROFESSIONAL

Project Management Templates

120+ PM Templates Includes:
  • 50+ Excel Templates

  • 50+ PowerPoint Templates

  • 25+ Word Templates

Effortlessly Manage Your Projects

Seamlessly manage your projects with our powerful & multi-purpose templates for project management.

Share Post

The Property Get statement is an essential feature in VBA that allows developers to create custom read-only properties for their classes. This statement is often used in conjunction with the ‘Property Let’ or ‘Property Set’ statements, which allow for write-only or read/write properties, respectively.

In this blog post, we will explore the purpose and syntax of the Property Get statement, as well as provide the examples of its usage. We will also discuss some important notes and remarks to keep in mind when using this statement.

VBA Property Get Statement

Purpose of Property Get Statement

The Property Get statement serves the purpose of creating a custom read-only property for a class. It allows external code to retrieve the value of a private variable within the class without directly accessing it. This promotes encapsulation and protects the integrity of the value as it can only be modified by the class itself.

Syntax of Property Get Statement

The basic syntax for a Property Get statement is as follows:

Property Get PropertyName As DataType
    'code block
End Property

The ‘PropertyName’ is the name of the property to be created, and the ‘DataType’ represents the data type of the property. Inside the code block, the value of the property is set using the ‘PropertyName’ followed by the keyword ‘Get’. For example, if the property name is “Name,” the code inside the Property Get statement would be “Name Get.”

Examples of  VBA Property Get Statement

1. Creating a custom property for storing a person’s name in a class:

Private pName As String
Property Get Name() As String
    Name = pName
End Property

2. Defining a property to return the number of characters in a string:

Private pText As String
Property Get CharCount As Integer
    CharCount = Len(pText)
End Property

3. Using the Property Get statement to create a read-only property for returning the current date:

Property Get CurrentDate As Date
    CurrentDate = Date
End Property

4. Defining a property to return the sum of two private variables in a class:

Private pNum1 As Integer
Private pNum2 As Integer
Property Get Sum As Integer
    Sum = pNum1 + pNum2
End Property

5. Creating a property to return a range of values using a user-defined function:

Private pNumbers As Variant
Property Get EvenNumbers() As Variant
    Dim result As Variant
    Dim i As Integer
    
    For i = 1 To UBound(pNumbers)
        If pNumbers(i) Mod 2 = 0 Then
            result(i) = pNumbers(i)
        End If
    Next i
    EvenNumbers = result
End Property

Important Notes and Remarks

Here are some important points to keep in mind when using the Property Get statement:

  • It is good practice to use the keyword ‘Get’ before the property name when inside the code block.
  • Using Property Get without a matching ‘Property Let’ or ‘Property Set’ statement will create a read-only property.
  • The data type of the property in Property Get statement must match the data type of the variable being assigned in the code block.
  • If there is a need to modify the property value, the ‘Property Let’ or ‘Property Set’ statement must be used instead.
  • VBA does not allow for overloading of properties, so multiple ‘Get’ statements for the same property name cannot exist.

In conclusion, the Property Get statement plays a crucial role in creating read-only custom properties for classes in VBA. It helps promote encapsulation and ensures the integrity of the data being accessed. In this post, we discussed the purpose and syntax of the Property Get statement, provided the examples of its usage, and discussed some important notes and remarks to keep in mind. I hope this post was informative and helpful for your understanding of the Property Get statement in VBA.

  1. Did this blog post provide a clear understanding of the Property Get statement in VBA?
  2. Were the examples provided helpful in grasping the concept?
  3. Do you have any suggestions for improving this blog post or any topics you would like to see covered in the future?

Your feedback and views are valuable to us, so please do not hesitate to leave a comment below. Thank you for reading!

Effortlessly Manage Your Projects and Resources
120+ Professional Project Management Templates!

A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.

Save Up to 85% LIMITED TIME OFFER
Project Management Templates

All-in-One Pack
120+ Project Management Templates

Essential Pack
50+ PM Templates

Excel Pack
50+ Excel PM Templates

PowerPoint Pack
50+ Excel PM Templates

MS Word Pack
25+ Word PM Templates

Ultimate Project
Management Template
Ultimate Resource
Management Template
Project Portfolio
Management Templates
Categories: VBA StatementsTags: , Last Updated: September 28, 2023

Leave A Comment