top of page
  • John

Refactor / rename in VBA

Updated: Mar 22, 2023

One of the functions that is provided by VBE_Extras is the ability to rename a declaration and all the references of that declaration. So, if you create the following code:

Then put the cursor on any of the 'Foo's (either in the declaration – the Dim line – or in either of the subsequent references) then select Refactor > Rename, you can choose a new name that will be automatically applied to the declaration and the references.

Sounds a lot like 'find/replace all'? Well, the rename function is a lot cleverer than that! It actually parses the code of the VBA Project to find all of the references of the declaration being renamed and renames them all at once. If the same name is re-used in the same VBA Project for another declaration, the rename function can work this out and distinguish between the two, for example, if you create the following code (I'm not saying this is 'good' code, but it is 'runnable' code):

… and then rename the Foo that is a Sub, it will (correctly) rename only the declaration and one reference of that Sub.

And if you rename the Foo that is a variable …

… it will (correctly) rename only the declaration and the two references of that variable. The result is the following, which is not the result you would get with 'find/replace all':

As well as that:

  • While entering the new name, VBE_Extras will also check that it doesn't clash with that of an existing declaration in the same scope, and check that it doesn't clash with a reserved word.

  • If the declaration that you are renaming is a Property then any matching Property (i.e. Let/Set for a Get; Get for a Let/Set) will be renamed. Also, VBE_Extras will offer to rename any backing module-level Variable or Type element where a match could be identified (see the VBE_Extras guide for more details).

  • If the declaration is a Getter or Setter then any matching Getter or Setter will be renamed and again VBE_Extras will also offer to rename any backing Variable or Type element with the same rules as above for a Property.

  • In the reverse situation, if the declaration is a backing member itself, then VBE_Extras will offer to also rename the related Properties or Getter/Setters – again applying the same rules (again, see the VBE_Extras guide for more details).

  • Attribute names for Module-level members (Procedures, Properties, variables and constants) are also updated (to read more about Attributes are, see my VBA Attributes blog post).

  • If renaming a Control (on a UserForm or, in Access, a Form or Report) then the Control's event handlers will also be updated to match the new name

The rename function supports renaming all types of declarations including:

  • The VBA Project

  • Modules (document, standard, Class and UserForm) … but not ThisOutlookSession in Outlook

  • Enums and Enum members (including 'square bracket' Enum member names)

  • Types and Type elements

  • Procedures and Properties

  • 'Declare' Procedures (ie those that access the Windows API)

  • Events

  • UserForm Controls

  • Variables (Module-level and Procedure/Property-level)

  • Constants

  • Line labels

The rename function also has a matching undo function, so long as you have not made any major edits to your Project since doing the rename then you can use this to undo the last rename.

Much more than 'find/replace all'!



bottom of page