VBA ME

Me Keyword in VBA Excel

I am sure you must have had this question “What is “ME” in VBA? Yes, even I had this question when I was new to VBA. After spending a considerable amount of time, I have experienced the ME keyword in excel VBA Coding. It is a bit advanced level if you are the starter in VBA. Anyway, you will get the hang of it slowly. In this article, we will show you how to use the “Me” keyword in Excel VBA coding.

“ME” is the object in VBA, which is specially designed which is built-in to excel. It points to the object which resides in, and we can call that object by the “ME” keyword. “ME” represents the parent object from where the code resides.

If you do not understand anything technically, don’t worry because when the examples come, you will get to know this better. Before that, let me give some background in VBA.

When we write excel macros, we write in “Modules,” and in modules, we have two sets of modules. The first one is “Standard Modules,” and the second one is “Class Modules.”

Further, in VBA Class Modules, we have two subcategories, i.e., Module with user Interface Element and Module without Interface Element. But for this example purpose, we will take into consideration only “Module with User Interface Element.”

Some of the examples for these modules are ThisWorkbook, Sheet1, UserForm1, and so on.

This is the general overview of the ME keyword in VBA. Now we will see practical examples of ME word.

VBA-Me

You are free to use this image on your website, templates etc, Please provide us with an attribution linkHow to Provide Attribution?Article Link to be Hyperlinked
For eg:
Source: VBA ME (wallstreetmojo.com)

How to Use ME in Excel VBA?

For example, look at the below code in Module 1.

vba me example 1.1

Remember, this is the code I had written in Module 1. This will insert the word “Hello Friends” to the worksheet named “Data Sheet.”

vba me example 1.2

In order to refer to the sheet “Data Sheet,” we have used worksheets object and entered the worksheet by its name.

Now I will double click on “Data Sheet.”

vba me example 1.3

As soon as we double click we can see the blank coding window on the right-hand side.

Now start the VBA subprocedure.

Code:

Sub Me_Example()

End Sub
vba me example 1.4

Now since I am writing the code in this sheet only, instead of mentioning the worksheet name, I can just call this as “ME.”

vba me example 1.5

We can see the IntelliSense list with ME word. Now the word “ME” works like an implicitly declared object variable.

Now using ‘VBA Me’ lets access the Range object in VBA and insert the same word as above in the sheet.

Code:

Sub Me_Example()

    Me.Range("A1").Value = "Hello Friends"

End Sub
vba me example 1.6

This will insert the same word as our previous macro in the excel sheet.

vba me example 1.2

Here one thing we have noticed is we can see the word “ME” only in the specific objects, and that word represents the object where the code writing is going on.

In this example, the Me keyword represents the worksheet “Data Sheet.”

Below are some of the example codes of ME word.

You can download this VBA Me Excel Template here – VBA Me Excel Template

Example #1

Code:

Sub Me_Example()

Me.Range("A1").Value = "Hello Friends"
'This will insert Hello Friends to the cell A1 in a Data Sheet.

Me.Name = "New Sheet"
'This will change the sheet name from the Data Sheet to New Sheet.

Me.Select
'This will select the sheet.

End Sub
vba me example 2

When we run the above code using the F5 key, in A1 cell, we’ll get Hello Friends, sheets name will change from Data Sheet to New Sheet, and this sheet will get selected.

vba me example 2.1

Example #2 – VBA ME with User Form

“ME” is the keyword often used with User forms in VBA. For example, look at the below image of the newly inserted user form in VBA.

Example 3.1

This user form name is “UserForm1”.

Whenever we want to address this user form from another module, we can call this by this name, i.e., “UserForm1”.

But when we work within this user form, we don’t need to rely on the name of the user form. Rather we can just use the word “Me.”

Let’s draw one simple text box on the user form.

Example 3.2

Double click on User Form to see the below macro.

Example 3.3

Now to insert text to the newly inserted text box, we can use two methods.

#1 – First, we can address the User Form by its name and text box by its name.

Code:

Private Sub TextBox1_Change()

UserForm1.TextBox1.Text = "Welcome to VBA!!!"

End Sub
Example 3.4

UserForm1 is the name of the user form. TextBox1 is the name of the text box. So we have inserted the text “Welcome to VBA!!!”.

#2 – Since we are writing the code in the same user form, we can call this by “ME.”

Code:

Private Sub TextBox1_Change()

Me.TextBox1.Text = "Welcome to VBA!!!"

End Sub
Example 3.5

This will also do the same thing as the above code.

Example 3.6

Like this, in VBA, we can reference the object with the word “ME” when we specifically write the code in those objects.

Recommended Articles

This has been a guide to VBA Me. Here we discuss how to use the ME keyword in VBA excel along with the examples and downloadable excel template. You can learn more about VBA Excel from the following articles –

  • 3 Courses
  • 12 Hands-on Projects
  • 43+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>