Calling a macro in different modules

Asked

Viewed 8,769 times

2

Guys 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 run everything at once?

follows the Cod I found but did not work

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

Sub dahs01()
    Call Module6.Dash_01
End Sub
  • Please read the How we should format questions and answers? to see how to format the code. And for larger codes you can use this site to carry out identation.

  • Done! Thanks for the identation tip

  • How are they declared? Are they public or private? If they are Public, simply call the function name or Sub in the Sub run, typing the Sub name, without the need to specify the module. And there’s no need to use the Call. And they’re in the same workbook?

  • They’re like Sub, they’re public

1 answer

3


Example

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

Árvore de Projeto VBA

So, assume that all the programming is in the same Workbook.

In Module 1 there will be the Sub executar()

In Module 2 the following code:

Sub teste1()
    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 executing 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, for he is a Private Sub of mill3.

But when executing 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 if test 1() and test 2() 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

Therefore, it is necessary to call the Subs in another Sub to accomplish this.

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

  • Excellent! thanks for the help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.