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

Get VB & VBA in a Nutshell: The Language now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.