| Me Operator |
Syntax
Me
Description
The Me operator can represent a class or a form, but only from within that class or form.
Rules at a Glance
Me is an implicit object reference to the current object module—either a Form, a UserForm, or a Class module.
The Me operator is particularly useful when passing an instance of the currently executing class as a parameter.
Example
In this example, a class method passes an instance of itself to another class using the Me operator:
Public Function ChangeName(NewName As String) As Boolean
Dim oMain As Main.Utils
If IsUnique(NewName) Then
msName = NewName
Set oMain = New Main.Utils
oMain.Save Me
Set oMain = Nothing
ChangeName = True
Else
ChangeName = False
End If
End Function
Programming Tips and Gotchas
Unfortunately, unlike the implementation of the Me operator in other languages, the VB/VBA version can't refer to an individual control.
The Me operator can't be used on the left side of an expression.
Another favorite use of the Me keyword is within Form modules, when unloading the Form or UserForm. For example:
Private Sub mnuExit Unload Me End Sub
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access