Call up a macro in different modules

1

Good morning, I created several macros, but I want to unify everything, the same are in different modules.

How do I put them together and execute everything at once?

Sub executar()
    MsgBox "Mensagem de aviso que ira iniciar"
    Call dahs01
End Sub

Sub dahs01()
    Call Module6.Dash_01
End Sub
    
asked by anonymous 24.05.2018 / 14:44

1 answer

2

Example

A simple example will be demonstrated, with the following tree in VBA:

So,assumethatalltheprogrammingisinthesameWorkbook.

Inmodule1therewillbeSubexecutar()

Inmodule2thefollowingcode:

Subteste1()MsgBox"1"
End Sub

And in module 3:

Sub teste2(text As String)
    MsgBox text
End Sub

Private Sub teste3()
    MsgBox "3"
End Sub

Explanation

When running with the following code:

Sub executar()
    Call teste1                                  'Não é necessário o Call, somente o VBA ainda utiliza esta sintaxe

    teste2 "2"

    teste3
End Sub

An error will occur in teste3 , since it is a Private Sub of module3.

But when running with:

Sub executar()
    Call teste1                                  'Não é necessário o Call, somente o VBA ainda utiliza esta sintaxe

    teste2 "2"
End Sub

The code will work correctly, because even though test1 () and test2 () are in other modules, they are declared as Public.

To run a Private Sub from another module, use:

Sub executar()
    teste1

    teste2 ("2")

    Application.Run "Módulo3.teste3"
End Sub

So it is necessary to call Subs in another Sub to accomplish this.

  

For more advanced code, refer to this reference: Programming The VBA Editor . In that you can perform actions within the VBA Project, being able to list the Subs of the modules and give run.

    
24.05.2018 / 17:00