Search code examples
excelvbabooleanprocedures

Affecting different Procedures by changing values


I have this master procedure that calls other procedures. But at two places, I need to check that all values are correct. If they are not, I want this master procedure to quit. What I am trying to do is check the values in my sub procedures and if they are not correct, change exitall to true, which would cause the sub procedure to stop. The thing is, I am fairly certain that if I say in my subprocedure to change the value of of exitall to true, it will not affect my master procedure.

My question is, how do I make the exitall to change in my master procedure, if it is changed in my subprocedure?

Thank you.

Sub Allstepstogether()
 Dim r As Integer
 Dim exitall As Boolean
 Dim answer As Variant
 Dim hda As Boolean
 Dim wdfh As Variant

 hda = False
 exitall = False

 Call Clean

 For Each cell In ThisWorkbook.Sheets("Heyaa").Range("C2:C15")
     If Weekday(Date) = vbMonday Then
         If CDate(cell.Value) = Date - 3 
            hda = True
            wdfh = cell.Offset(0, 1).Value
         End If
     Else
         If CDate(cell.Value) = Date - 1 Then
             hda = True
             wdfh = cell.Offset(0, 1).Value
         End If
     End If
 Next cell

Call step4

 r = Application.WorksheetFunction.CountA(ThisWorkbook.Sheets("BlaCheck").Range("A1:A150"))
 If r <> 100 Then
     answer = MsgBox("Data not yet uploaded, try again later.", vbOKOnly)
     Exit Sub
 Else
     Call step5
     If exitall = True Then
         Exit Sub
     Else
         Call Step7alloptions
         Call step8
         Call Timetocheck
         If exitall = True Then
             Exit Sub
         Else
             Call Step9
             Call Step10
             Call Step11
         End If
     End If
 End If
 end sub

Part of Step5, which should change exitall to true thus stopping the master procedure to be executed if it is incorrect.

  sub Step5
  dim exitall as boolean
  dim lr as integer

  '....
  'code
  '....

  lr = Application.WorksheetFunction.CountA(ThisWorkbook.Sheets("BlaCheck").Range("A1:A500"))
  If lr > 100 Then
     answer = MsgBox("Ups, It did not run correctly, this code execution will be terminated", vbOKOnly)
     exitall = True
     Exit Sub
  End If
  end sub

Solution

  • Option 1 is to declare exitall with global scope:

    Dim exitall As Boolean    '<--Add this at the top of the module.
    
    Sub Allstepstogether()
        Dim r As Integer
        'Dim exitall As Boolean  '<--Remove this from all your Subs.
    

    Better would be to change your Subs into Functions that return a Boolean for success and just test for that. Note that you also don't have to use Else after an If condition that exits - that should cut down your indentation level dramatically:

    Converted Sub:

    Function Step5() As Boolean
        Dim lr As Integer
    
        '....
        'code
        '....
    
        lr = Application.WorksheetFunction.CountA(ThisWorkbook.Sheets("BlaCheck").Range("A1:A500"))
        If lr > 100 Then
           answer = MsgBox("Ups, It did not run correctly, this code execution will be terminated", vbOKOnly)
           Exit Function
        End If
        Step5 = True
    End Function
    

    Calling code:

    Sub Allstepstogether()
        '[Snip]
        r = Application.WorksheetFunction.CountA(ThisWorkbook.Sheets("BlaCheck").Range("A1:A150"))
        If r <> 100 Then
            answer = MsgBox("Data not yet uploaded, try again later.", vbOKOnly)
            Exit Sub
        End If
        If Not Step5 Then Exit Sub
        Step7alloptions
        step8
        If Not Timetocheck Then Exit Sub
        Step9
        Step10
        Step11
    End Sub