Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vba Show references to other procedures

Today i want to ask this question. say i have a module and in that module i call different procedures (subs or function ) which are located in other modules, how can i list all the procedure names and their module or class in a simple way in case i wanted to copy those procedures to the current module and delete these modules. thanks

i don't know how to achieve that but here is a dummy code

for each UserDefinedProcedure in UserProcuduresCollection
if UserDefinedProcedure.Name (is Found in this module) then
debug.print UserDefinedProcedure.Name
end if 
next 
like image 243
Ali_R4v3n Avatar asked Mar 23 '16 16:03

Ali_R4v3n


People also ask

How do you display References dialog box?

On the Tools menu, click References to display the References dialog box.

How do you call a sub procedure in Excel VBA?

To call a Sub procedure from another procedure, type the name of the procedure and include values for any required arguments. The Call statement is not required, but if you use it, you must enclose any arguments in parentheses. Use a Sub procedure to organize other procedures so they are easier to understand and debug.

How do I enable References in VBA?

Add A Reference Make sure that you click on the workbook you want to add the reference to, and from the VBA editor menu choose Tools -> References. In the displayed list check the box beside your renamed add-in, and then click on OK. You'll see that your workbook now has a new reference to the add-in.

How do I find References in Excel VBA?

The VBA reference list (figure 1) can be accessed by the VBE menu Tools > References sequence. The Available References list displays each reference item by its description property, in priority order.


1 Answers

DISCLAIMER: I wrote this part of the Rubberduck add-in myself, and I own and maintain the Rubberduck project.


Download and install Rubberduck. Right-click the procedure name (from its declaration, or any of its usages) and select the "Find all references" command from the code pane context menu:

Rubberduck 1.4.3 Find all references

You can then double-click an item in the list to navigate there.

The latest release dates a little (July 2015), but the project is well alive on GitHub, and version 2.0 should be released in 6-8 weeks. It won't let you iterate procedure/identifier references with VBA code, but a COM API to do exactly that is on the project's roadmap.

Rubberduck 2.0's "find all references" can find references to pretty much anything, from classes:

Rubberduck 2.0's search results toolwindow

To properties:

references to 'Cancelled' property accessor

...and library functions:

references to MsgBox


Doing this with code, even very smart code, using the VBIDE API, isn't going to be reliably possible. The reason for this is that it's perfectly legal to have this code in Module1:

Sub DoSomething()
    'do something
End Sub

And then this code in Module2:

Sub DoSomething()
    'do something
End Sub

To correctly resolve references to DoSomething inside Module1, you need to check whether the call is qualified (e.g. Module2.DoSomething will call the procedure inside Module2 - an unqualified call will call the procedure inside Module1).

Resolving identifier references is something I've spent pretty much an entire year refining the code for, and it's still not perfect (although it now resolves crazy ambiguous code you wouldn't even think is legal VBA) - doing that in plain VBA is suicidal at best.

Eventually we'll expose a COM API that will allow Rubberduck users to write VBA code something like this completely hypothetical code:

For Each declaration In RubberduckParserState.UserDeclarations
    If declaration.DeclarationType = Procedure And declaration.ParentScope = "VBAProject.Module1" Then
        For Each reference In declaration.References
            Debug.Print declaration.IdentifierName " used in " & reference.ToString
        Next
    End If
Next
like image 168
Mathieu Guindon Avatar answered Nov 15 '22 13:11

Mathieu Guindon