VBA – How to Rename or Delete a Module or Form

How to Rename or Delete a VBA module or Form

The first time you insert a module in VBA, it will automatically be given the name of “Module1” and subsequent module will become Module2, Module3 etc. Similarly, when you insert a UserForm it will be called UserForm1, UserForm2 etc.

VBA 15 PIC 01

Renaming a Module

To rename your module or form, you need to have the properties window switched on in your VBE.

If the properties windows is not showing, press F4 or go to the menu bar and select View > Properties Window.

VBA 15 PIC 02

  1. Click on the module you wish to rename in the Project Explorer.
  2. Select the name property in the Properties Window (a module will only have this property; a form has multiple properties)VBA 15 PIC 03

3. Delete the module name (in this case Module1) and type in the name you want to call your module.

VBA 15 PIC 04

4. Press enter to rename the module.

VBA 15 PIC 05

You rename your forms in the same manner.

Delete a Module

Occasionally you may have the need to remove a module or form that you no longer need.

Right-click on the module or form you wish to remove to show the right click short cut menu.

VBA 15 PIC 06

Click Remove (in this case Module2)

OR

Click on the File menu, and then click on Remove (Module2).

VBA 15 PIC 07

A warning box will appear asking if you want to Export the module or form before you remove it. Exporting the module or form enables you to save it as an individual file for use in a different Excel project at some other time.

VBA 15 PIC 08

More often than not when you remove a module or form it is because you do not need it, so click No.

Name Modules & Forms

It is good practice to rename modules and forms in order to give them more meaningful names. For example, if UserForm1 is going to be a form for entering invoice details, we can rename it to frmInvoices.  Similarly, if Module1 is going to be used to hold some general functions that are going to be used repetitively throughout your code, you could rename it to basGeneral or modGeneral (depending on which naming conventions you like to use).

It is a good idea to be consistent with the naming conventions, both in naming your modules and forms, as well as the variables that you use within your code.  You will notice that I have written the first 3 letters of the module or form name in lowercase, and have a capital letter at the beginning of the description for the module or form.  This is known as CamelCase (https://en.wikipedia.org/wiki/Camel_case) and is a good habit to  get into when naming your objects. I have also used the Leszynski naming convention which is often used by Visual Basic programmers. (https://en.wikipedia.org/wiki/Leszynski_naming_convention).