Do not trigger next sub if exit flags in 1st sub are triggered

shapeshiftingkiwi

New Member
Joined
Mar 31, 2021
Messages
33
Office Version
  1. 365
Platform
  1. Windows
I can't figure out why the Print_Only2 sub is running when the exit flags are being triggered. Also, what would be the proper logical operator to have Print_Only2 not run if ExitFlag1 is true OR ExitFlag2 is true, OR both are true?

Any help much appreciated, I feel like I'm close.

VBA Code:
Public Sub Save_Then_Print()
    Save_As5
    If Exitflag1 = False Or Exitflag2 = False Then
        Print_Only2
    End If
End Sub


Private Sub Save_As5()
   Dim Exitflag1 As Boolean, Exitflag2 As Boolean
    Exitflag1 = False
    Exitflag2 = False
    Dim ErrorCells As String
    ErrorCells = ""

    For Each cell In ActiveSheet.Range("F14:F37")
        If cell.EntireRow.Hidden = False And cell.value = "" Then
            Exitflag1 = True
            ErrorCells = ErrorCells & cell.Offset(0, -2).value & ", "
        End If
    Next cell

    For Each cell In ActiveSheet.Range("G14:G16,G18:G37")
        If cell.EntireRow.Hidden = False And cell.value = "" Then
            Exitflag2 = True
            ErrorCells = ErrorCells & cell.Offset(0, -3).value & ", "
        End If
    Next cell

    If Exitflag1 = True And Exitflag2 = True Then
        MsgBox "missing information for " & ErrorCells
    ElseIf Exitflag1 = True Then
        MsgBox "missing lot number for " & ErrorCells
    ElseIf Exitflag2 = True Then
        MsgBox "missing quantity for " & ErrorCells
    Else
        With Application.FileDialog(msoFileDialogSaveAs)
           .Title = "Save"
           .ButtonName = "Save Form"
           .Application.DisplayAlerts = False
           .InitialFileName = "P:\COMPANY - Production\Production Form Save As Test\" & Range("Save_As!T3").value
           If .Show Then
              .Execute
           End If
        End With
    End If
End Sub

Private Sub Print_Only2()
        ActiveSheet.PrintOut preview:=True
End Sub
 
Last edited by a moderator:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Ended up using AI to make it work.

VBA Code:
Public Sub Save_Then_Print()
    Dim Exitflag1 As Boolean, Exitflag2 As Boolean
    Save_As5 Exitflag1, Exitflag2
    If Exitflag1 = False And Exitflag2 = False Then
        Print_Only2
    End If
End Sub
Private Sub Save_As5(ByRef Exitflag1 As Boolean, ByRef Exitflag2 As Boolean)
    Exitflag1 = False
    Exitflag2 = False
    Dim ErrorCells As String
    ErrorCells = ""

    For Each cell In ActiveSheet.Range("F14:F37")
        If cell.EntireRow.Hidden = False And cell.value = "" Then
            Exitflag1 = True
            ErrorCells = ErrorCells & cell.Offset(0, -2).value & ", "
        End If
    Next cell

    For Each cell In ActiveSheet.Range("G14:G16,G18:G37")
        If cell.EntireRow.Hidden = False And cell.value = "" Then
            Exitflag2 = True
            ErrorCells = ErrorCells & cell.Offset(0, -3).value & ", "
        End If
    Next cell

    If Exitflag1 = True And Exitflag2 = True Then
        MsgBox "missing information for " & ErrorCells
    ElseIf Exitflag1 = True Then
        MsgBox "missing lot number for " & ErrorCells
    ElseIf Exitflag2 = True Then
        MsgBox "missing quantity for " & ErrorCells
    Else
        With Application.FileDialog(msoFileDialogSaveAs)
           .Title = "Save"
           .ButtonName = "Save Form"
           .Application.DisplayAlerts = False
           .InitialFileName = "P:\COMPANY - Production\Production Form Save As Test\" & Range("Save_As!T3").value
           If .Show Then
              .Execute
           End If
        End With
    End If
End Sub
Private Sub Print_Only2()
        ActiveSheet.PrintOut preview:=True
End Sub
 
Last edited by a moderator:
Upvote 0
Solution
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags in both posts for you this time. 😊
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top