VBA ME

VBA ME

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 CodingVBA CodingVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task.read more. 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.

“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 macrosExcel MacrosA macro in excel is a series of instructions in the form of code that helps automate manual tasks, thereby saving time. Excel executes those instructions in a step-by-step manner on the given data. For example, it can be used to automate repetitive tasks such as summation, cell formatting, information copying, etc. thereby rapidly replacing repetitious operations with a few clicks. read more, 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 ThisWorkbookThisWorkbookVBA ThisWorkbook refers to the workbook on which the users currently write the code to execute all of the tasks in the current workbook. In this, it doesn’t matter which workbook is active and only requires the reference to the workbook, where the users write the code.read more, 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.

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.

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.

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

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

Now start the VBA subprocedureVBA SubprocedureSUB in VBA is a procedure which contains all the code which automatically gives the statement of end sub and the middle portion is used for coding. Sub statement can be both public and private and the name of the subprocedure is mandatory in VBA.read more.

Code:

Sub Me_Example() End Sub

Since we are only writing the code in this sheet, instead of mentioning the worksheet name, we can call this “ME.”

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 VBARange Object In VBARange is a property in VBA that helps specify a particular cell, a range of cells, a row, a column, or a three-dimensional range. In the context of the Excel worksheet, the VBA range object includes a single cell or multiple cells spread across various rows and columns.read more and insert the same word as above in the sheet.

Code:

Sub Me_Example() Me.Range(“A1”).Value = “Hello Friends” End Sub

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

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 Sub

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.

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 VBAUserForm In VBAIn VBA, userforms are customized user-defined forms that are designed to accept user input in the form of a form. It has various sets of controls to add such as text boxes, checkboxes, and labels.read more.

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.

Double click on UserForm to see the below macro.

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 Sub

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 Sub

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

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 article has been a guide to VBA Me. Here, we discuss using the ME keyword in VBA Excel and the examples and downloadable Excel template. You can learn more about VBA Excel from the following articles: –

  • Declare Array in VBA
  • DateSerial in VBA
  • VBA Collection Object
  • VBA ChDir Function
 

Related Posts