5

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 
Ali_R4v3n
  • 377
  • 5
  • 15
  • 1
    Have a look at [Programming The VBA Editor](http://www.cpearson.com/excel/vbe.aspx). With ```VBA Extensibility library``` you can do almost everything. – Daniel Dušek Mar 23 '16 at 17:09
  • @Mat's Mug, it would be great if you can come up with some code and thanks for your comment .. – Ali_R4v3n Mar 23 '16 at 17:14

2 Answers2

5

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
Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
  • 5
    THANK YOU, I really appreciate your contribution to the world by giving us The Rubberduck :), i was actually looking for something that beefs up the VBE and adds more functionalities to it. cheers – Ali_R4v3n Mar 23 '16 at 17:52
0

In break mode, press Ctrl+L to view the call stack. This is the best you're gonna get in the VBE.

Note that the call stack cannot be accessed via code and so what appears to be your end-goal cannot be achieved in VBA.

SierraOscar
  • 17,507
  • 6
  • 40
  • 68