Me Operator |
Me
The Me operator can represent a class or a form, but only from within that class or form.
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.
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
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