A Macro to run all Modules?

Asked

Viewed 2,153 times

4

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

EXAMPLE OF ONE OF THE MODULES WITH SEVERAL ROUTINES:

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?

1 answer

2


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

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

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

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

And the one that spins 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 function instead of sub there are some differences, but basically two:

  1. The functions do not appear to the user when in the spreadsheet tries to run a macro, for example, being in the spreadsheet the user can press Alt+F8 to show macros and try to run some, if it’s function instead of sub, it won’t be there.

  2. The functions can receive and return values and could be better explored in the code, thus:

    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 by spreadsheet, 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

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

Browser other questions tagged

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