Example
A simple example will be demonstrated, with the following tree in 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.
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.
– danieltakeshi
Done! Thanks for the identation tip
– John Hebert
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?– danieltakeshi
They’re like Sub, they’re public
– John Hebert