![visual basic for excel module example visual basic for excel module example](https://www.puresourcecode.com/wp-content/uploads/2020/10/image-19-1024x555.png)
#VISUAL BASIC FOR EXCEL MODULE EXAMPLE CODE#
This allows you to use Me throughout the UserForm code without having to worry about the actual name of the UserForm in order to reference it.Ĭlick the Close Me button and the form will close using the code I posted above. Since the Me keyword is used within a UserForm here, it refers to that specific UserForm. Me refers to what should be closed, this UserForm. In the window that opens, I'll add a small piece of code that is attached to the only command button in this particular form: Private Sub CommandButton1_Click()
![visual basic for excel module example visual basic for excel module example](https://www.excelanytime.com/excel/images/ExcelVBA/VBE/vbe_1.gif)
Go to the VBA Editor (Alt + F11), double-click the module for your form and right-click on the form and click View Code. This makes your code more versatile because you don't have to change anything if the name of the UserForm changes. You use the Me keyword inside a UserForm in order to reference that form. The Me keyword also works the same way in your workbook event macros. You can see that it returns the name of the current Excel workbook. Since the macro is in the ThisWorkbook module, its name will start with ThisWorkbook.
![visual basic for excel module example visual basic for excel module example](https://i.ytimg.com/vi/DlXNNHTcu4E/maxresdefault.jpg)
Now, go back to Excel and hit Alt + F8 and choose the macro from the list to run it. MsgBox just outputs the result in a pop-up window so we can see it. Here, I'll just get the name of the workbook and output that in a message box. there will be a list of options that show you the data you can access. Go to the VBA Editor (Alt + F11) and double-click where it says "ThisWorkbook" and in the window that opens, input your macro code. Using the Me keyword within the ThisWorkbook module allows you to access data related to the workbook. The Me keyword also works the same way in your worksheet event macros. No matter where you run this macro, the output will always be the name of Sheet1, which is "Sheet1" this is because the code for the macro is placed within the worksheet module for Sheet1 and so it doesn't matter from where you call it. Now, run the macro (you don't have to be on Sheet1 to run this macro). Since the macro is in the worksheet module for Sheet1, its name will start with Sheet1 MsgBox simply outputs this in a pop-up window so we can see the result. Now, let's get the name of the sheet and output that in the message box: Sub me_sheet() you will see a list of options that shows you the kinds of data that you can access:Īll of this data is specific to the worksheet in which you are creating this macro so, I created the macro in Sheet1 (also referred to as the worksheet module for Sheet1) and that means that all of the data from the Me keyword here will be for Sheet1. Then let's start with a very simple macro that will output a message using data from the Me keyword. Go to the VBA Editor (Alt + F11) and double-click Sheet1 or any sheet. Using Me within a worksheet module allows you to access data related to that worksheet. This may seem a little confusing, so let's get to some examples. The VBA code can still reference data from anywhere in Excel but the scope of the Me keyword is limited to where it is placed. Note: You cannot use the Me keyword inside of a regular module!Īny code that is put into a worksheet module is specific to that worksheet putting code into the workbook, via ThisWorkbook, is specific to the workbook in which it is placed and putting Me inside the code for a UserForm refers to that UserForm. You can use the Me keyword inside of a worksheet module, a workbook module, and a UserForm. This allows you to make more versatile macros that won't break when you rename something or copy them to another workbook or worksheet. The Me keyword in Excel VBA allows you to refer to either the current worksheet, workbook, or userform without having to use their name.