Search code examples
vbaexcelsubroutine

Running at once multiple Subs from an unique VBA Sub


I have three different Subs available in a VBA module and wanted to call those series of Subs from an unique Sub activated through a VBA button.

Below the code running:

Sub Updateworkbook()

Call Unprotectworkbook
Call CopyAndPaste
Call Protectworkbook

End Sub

After the first Sub Unprotectworkbook() is run the other Sub are not called and executed. Why this happens?

Below the Unprotectworkbook() Sub code for your reference

Sub Unprotectworkbook()

 Dim myCount
    Dim i
    myCount = Application.Sheets.Count
    Sheets(1).Select
    For i = 1 To myCount
        ActiveSheet.Unprotect "password"
        If i = myCount Then
            End
        End If
        ActiveSheet.Next.Select
    Next i

End Sub

Solution

  • Modify your code as follows (change End to Exit Sub):

    Sub Unprotectworkbook()
    
     Dim myCount
        Dim i
        myCount = Application.Sheets.Count
        Sheets(1).Select
        For i = 1 To myCount
            ActiveSheet.Unprotect "password"
            If i = myCount Then
                Exit Sub
            End If
            ActiveSheet.Next.Select
        Next i
    
    End Sub
    

    or you can simply change it to the next one:

    Sub Unprotectworkbook()
        Dim sh   
        For Each sh In Sheets
           sh.Unprotect "password"
        Next 
    End Sub