A Macro to run all Modules?

4

I have several modules and within these modules I have several subroutines with some functions on separate sheets. I want to run the macros separately! That is, start the next sub routine after finishing the previous subroutine.

EXAMPLE OF ONE OF THE MODULES WITH VARIOUS ROTINES:

Sub um()
'código [...]
dois
End sub

Sub dois()
'código [...]
tres
End sub

Sub tres()
'código [...]
End sub

How to create a module that runs all macros?

    
asked by anonymous 07.06.2017 / 12:51

1 answer

2

All modules, functions, and subs are interconnected in Excel, could have each sub independent and have another function to run everything in sequence, like this:

>
Sub um()
'código [...]
End Sub

Sub dois()
'código [...]
End Sub

Sub tres()
'código [...]
End Sub

And the one that runs in the sequence:

Sub RodaTudo()

 ' Chama a Sub UM
 um

 ' Chama a Sub DOIS
 dois

 ' Chama a Sub tres
 tres

End Sub

Something I use a lot are functions instead of sub there are some differences, but basically two:

  • The functions do not appear to the user when the worksheet tries to run a macro, for example, in the worksheet the user can press Alt + F8 to show the macros and try to run some, if it is a function instead of sub, it will not be there.

  • Functions can receive and return values and could be better exploited in code, like this:

    Function um(ByVal TESTE As String) As Boolean
     'código [...]
     If TESTE = "Rodar Próxima" Then
       um = True
     End If
    End Function
    
    
    Function um(ByVal TESTE As String) As Boolean
     'código [...]
     If TESTE = "Rodar Próxima" Then
       um = True
     End If
    End Function
    
    Function DOIS(ByVal RODADOIS As Boolean) As Boolean
     'código [...]
     If RODADOIS Then
       DOIS = True
     End If
    End Function
    
    Function tres(ByVal DOIS As Boolean) As String
     'código [...]
     If DOIS Then
       tres = "Rodou tudo!"
     End If
    End Function
    
  • And a Sub to call the worksheet, validating the result of each function:

    Sub RodaTudo()
    
      ' Aqui chamará a Função UM, depois a DOIS (caso o resultado seja true) e depois a TRES (caso o resultado da DOIS seja true)
      tres (DOIS(um("Rodar Próxima")))
    
      ' Ou pode-se fazer testes
      If um("Rodar Próxima") Then ' Se verdadeiro rodar abaixo
        dois(true)
      End If        
    
    End Sub
    

    Q: Subs can only receive values, but do not return values directly.

        
    07.06.2017 / 13:14