top of page

VBE_Extras catch-up #5: References for code at cursor

  • Writer: John
    John
  • 2 days ago
  • 3 min read
When I first made VBE_Extras available for download, it contained a whole load of functionality that I never wrote blog posts about. I'm now setting that right for a few of the core areas of that functionality.

If the cursor is on a declaration, or on a reference of a declaration (sometimes a "reference" is called a "use" or even a "usage"), then the "References for code at cursor" command will display a dialog with a list of all references of that declaration (including the declaration itself). So it could be every place that a variable is referenced / used. Or every place that a procedure is referenced / used. Or a constant. Or an Enum, a Type ... or any other type of declaration as noted in the VBE_Extras catch-up #2: Listing all declarations post.


And just to be clear that this command doesn't simply find all occurrences of a name and just assume that they all relate to the same declaration ... rather it parses the VBA code to disambiguate between multiple declarations with the same name and then applies code-parsing rules to determine which references relate to which declaration (for an example of a some code with seven declarations using the same name all within the same Module, see Testing 'ThisThing' on YouTube which actually uses the Highlight command rather than the "References for code at cursor" command, but both commands use the same "declarations and references engine" within VBE_Extras and so both commands identify the same references).


Additionally, the command is also aware of whether the declaration is procedure-level, Module-level or Project-level and will look for references in the appropriate scope (including, for host applications that support multiple VBA Projects being open at once, in other VBA Projects that your Project may have a reference to, or is referenced from).


Once you have the "References for code at cursor" dialog on-screen, it will allow you to show or hide references in code comments, in strings and "return values" (i.e. when a value is being set to the name of a Function or Property Get to be returned to the calling code). The dialog will show a column displaying the Project that each reference is within if the references are contained within multiple Projects.


References for code at cursor

If you select a specific reference in the dialog, you then have the option to "go to" it ... VBE_Extras will display the Module that the reference is within and move the cursor to the line and column of the reference.


You can also generate a CSV file of all of the references.


Other information relating to the "References for code at cursor" command


The command will also find implementations of Interfaces, handlers of Events including for Controls on UserForms (and Reports and Forms in Access).


The command tests whether Class, Document and UserForm Modules have their VB_PredeclaredId attribute set to True, so allowing for 'static' access of members, and tests whether Class, Document and UserForm Modules have their VB_Exposed attribute set to True, so allowing other VBA Projects that reference the Project to 'see' the Module and its members. For more information on Attributes, see VBA Attributes.

 

You can control whether the declaration (or declarations if conditional compilation is involved) is shown at the top of the list or shown in the default order (being Project, Module-type, Module, line, column) in the VBE_Extras Settings.


The list of references also includes information on how the reference is being used (i.e. Get, Let or Set) in the 'Use' column. Note that some references are neither a Get, Let nor a Set e.g. the reference of a declaration (other than constants and parameters), references of Modules, references in Strings and code comments etc.

 

Also, in Excel only, Shapes that have an OnAction property that references a Sub or Function in a standard Module will be included in the list of references.


There are exclusions when getting references in Access. For full details, see the "Using VBE_Extras in Access" section of the VBE_Extras user guide (the key exclusions are that references in Access macros and references in expressions in Form, Report and Control events are not identified).

Comments


bottom of page