VBA ME

Publication Date :

Blog Author :

Download FREE VBA ME In Excel Template and Follow Along!
VBA Me Excel Template.xlsm

Table Of Contents

arrow

Me Keyword in VBA Excel

We are sure you must have had this question "What is "ME" in VBA? Yes, everyone has this question when one is new to VBA. After spending considerable time, we have experienced the ME keyword in Excel VBA Coding. It is a bit advanced if you are a starter in VBA. Anyway, you will get the hang of it slowly. This article will show you how to use the "Me" keyword in Excel VBA coding.

VBA ME
You are free to use this image on your website, templates, etc.. Please provide us with an attribution link

"ME" is the object in VBA, designed specially and built into Excel. It points to the object which resides. Therefore, we can call that object the "ME" keyword. The "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. But, before that, let me give some background on VBA.

When we write excel macros, we write in "Modules." 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, we will consider only "Module with User Interface Element."

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

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

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 we had written in Module 1. In addition, it will insert the word "Hello Friends" into the "Data Sheet" worksheet.

vba me example 1.2

To refer to the "Data Sheet" sheet, we have used the worksheets object and entered the worksheet by its name.

Now, we will double-click on "Data Sheet."

vba me example 1.3

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

Now start the VBA subprocedure.

Code:

Sub Me_xample() End Subvba me example 1.4

Since we are only writing the code in this sheet, instead of mentioning the worksheet name, we can call this "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,' let us 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 Subvba me example 1.6

Insert the same word as our previous macro in the Excel sheet.

vba me example 1.2

Here, we have noticed that 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.

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 Subvba me example 2

When we run the above code using the F5 key in the A1 cell, we will get "Hello Friends." The sheet's name will change from "DataSheet" 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 UserForm 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 do not need to rely on the name of the UserForm. Rather, we can use the word "Me."

Let us draw one simple text box on the UserForm.

Example 3.2

Double click on UserForm to see the below macro.

Example 3.3

We can use two methods to insert text into the newly inserted text box.

#1 - First, we can address the UserForm by its name and the text box by its name.

Code:

Private Sub TextBox1_Change() UserForm1.TextBox1.Text = "Welcome to VBA!!!" End SubExample 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 “ME.”

Code:

Private Sub TextBox1_Change() Me.TextBox1.Text = "Welcome to VBA!!!" End SubExample 3.5

It 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.