0

Please help with the following:

1) A code that sets a list of all macros of "Module3", and place this list in "Sheet5", starting in cell "E14" below.

2) Then, the code should run all the listed macros

I tried with a code that referred VBComponent, but I got an error.

11
  • you try with this code ? stackoverflow.com/questions/3922772/return-a-list-of-all-macros WD2000: Sample Macro to Return Macro and Procedure Names ? Jan 25, 2015 at 2:01
  • Thanks @JuanRuizdeCastilla, I tried it but appears an error. I 've already set the excel settings, inserted a new module, and added the function and the sub in there. However, it appears: "Compile error: User-defined type not defined", and "Dim pj As VBProject" is highlighted in blue in the VBA window.
    – Flavinho
    Jan 25, 2015 at 3:37
  • did you see my answer ?, I will edit because you need to add a reference, check my answer in a while please. Jan 25, 2015 at 4:03
  • Ok, check the new other two images. Jan 25, 2015 at 4:17
  • Thanks @JuanRuizdeCastilla. That specific problem is solved, however there are two issues now: 1) This code is calling all macros, I need to call macros of Module3 only, 2) I need to get the list of the macros of Module3 in "Sheet5", starting in cell "E14" below. Thanks!
    – Flavinho
    Jan 27, 2015 at 16:50

1 Answer 1

5

Based on my google search, I found the answer That I commented you , but They forgot and important thing, that is check and option to allow you run the macro.

First the Function to list all macros in excel and return and string separated by white space:

Function ListAllMacroNames() As String

Dim pj As VBProject
Dim vbcomp As VBComponent
Dim curMacro As String, newMacro As String
Dim x As String
Dim y As String
Dim macros As String

On Error Resume Next
curMacro = ""
Documents.Add

For Each pj In Application.VBE.VBProjects

     For Each vbcomp In pj.VBComponents
            If Not vbcomp Is Nothing Then
                If vbcomp.CodeModule = "Module_name" Then
                    For i = 1 To vbcomp.CodeModule.CountOfLines
                       newMacro = vbcomp.CodeModule.ProcOfLine(Line:=i, _
                          prockind:=vbext_pk_Proc)

                       If curMacro <> newMacro Then
                          curMacro = newMacro

                            If curMacro <> "" And curMacro <> "app_NewDocument" Then
                                macros = curMacro + " " + macros
                            End If

                       End If
                    Next
                End If
            End If
     Next

Next

ListAllMacroNames = macros

End Function

The next step, of well could be the first one, you need to change some configuration of the office (Excel) trustcenter, check the follow images:

Step 1:

Step 1

Step 2:

Step 2

Step 3 (Final) Check the option "rely on access to the data model project vba":

Final Step

Then you need to add this reference to your Excel:

step 2.5

Don't worry if you have another version of Microsoft Visual Basic for Applications Extensibility, in this case is 5.3. Check and then accept.Don't forget that you need to find that reference, there is no on the top of the list.

step 2.6

Finally you can invoke the ListAllMacroNames ( ) function With This other macro named execute () , Look That I 'm Validated That doesn't call the same macros (execute , ListAllMacroNames ) or could make an infinite loop.

Public Sub execute()
Dim AppArray() As String

AppArray() = Split(ListAllMacroNames, " ")

For i = 0 To UBound(AppArray)

temp = AppArray(i)

If temp <> "" Then

    If temp <> "execute" And temp <> "ListAllMacroNames" Then

    Application.Run (AppArray(i))


    Sheet5.Range("E" & i + 14).Value = temp

    End If

End If

Next i

End Sub

EDIT 2 Change "Module_name" in first method, to your desire module, and set the corret sheet name (in this case Sheet 5) in execute method.

2
  • it is still throwing that VBComponent error with visual basic 7
    – adi
    Oct 19, 2020 at 18:42
  • @adi, this i an answer for vba, not sure if vb 7 is the same or has the same behavior of vba... , maybe if you post a new question and all SO community can help u. Oct 21, 2020 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.