The Me Keyword

If you’ve ever asked a question about userforms in the newsgroups, you may have gotten an answer that includes the Me keyword. What the heck is Me anyway?

Me is a special object that’s built-in to Excel. Technically, it points to the object that’s represented by the class in which the code resides. Oh yeah, that’s clear now.

First, a little background. There are two types of modules, standard modules and class modules. And there are two types of class modules, those with a user interface element and those without. For our purposes, we will be talking about class modules that have a user interface element. You know these modules by the names ThisWorkbook, Sheet1, UserForm1 and the like.

When you create a userform, you are really creating a class module. The object represented by that class module is the userform. When you are coding in that module, you can use the Me keyword to refer to the userform object. Instead of typing

UserForm1.TextBox1.Text = “Hello”

you can type

Me.TextBox1.Text = “Hello”

Why? First, it saves a lot of typing (that’s a big deal to me – I’m lazy). The big advantage comes when you change the name of your userform. Changing the name from UserForm1 to FrmSayHello breaks all the code where you refer to UserForm1. But the Me keyword always refers to the proper object no matter what you do to it.

The other commonly used class modules, Sheet1 (and other sheet objects) and ThisWorkbook, work similarly. These class modules are used to code sheet and workbook level events like Worksheet_Change and Workbook_Open. In sheet object modules, Me refers to the sheet and in ThisWorkbook it refers to the workbook. Ready made object variables at your disposal, what could be better.

Start using Me instead of the literal object names and, at the very least, you can impress your co-workers.

Posted in Uncategorized

5 thoughts on “The Me Keyword

  1. Yes,

    but out of millions and millions of web pages NEVER and I mean NEVER has anyone explained if Me stands for anything. Does it stand for memory, me as in it’s me the instance, or just two random letters? Look and you will not find an answer even from MS.

Posting code? Use <pre> tags for VBA and <code> tags for inline.

Leave a Reply

Your email address will not be published.