Home » excel » excel – Need BeforeClose to work with multiple conditions

excel – Need BeforeClose to work with multiple conditions

Posted by: admin May 14, 2020 Leave a comment

Questions:

I need to add some code to the BeforeClose event so that the user is updated with the number of missing/incomplete fields that are needed before they can return the worksheet to me. The problem is if one of the conditions are false, the workbook closes rather than moving on to the next condition.

I’ve looked into this issue and I can’t find any examples anywhere with a BeforeClose event with multiple if conditions.

This is what I have so far:

Private Sub Workbook_BeforeClose(Cancel As Boolean)




If Range("CA1").Value > 0 Then

MsgBox Range("CA1").Value & " - ITEM(S) NEED LIST PRICE CORRECTION.  There are  " & Range("CA1").Value & "   item(s) that presently have DEALER COST > LIST PRICE.  Please fix pricing for these item(s) as indicated in Column AA."

Else

If Range("BO1").Value > 0 Then

MsgBox Range("BO1").Value & " - ITEM(S) - PRICE JUSTIFICATION MISSING. There are  " & Range("BO1").Value & "   item(s) that have a 0% price change to Dealer Cost or a negative price change.  These items are missing PLM price justification in the comments field. Please add your comments to Column T."

Else

If Range("BQ1").Value > 0 Then

MsgBox Range("BQ1").Value & "- ITEM(S) - NEGATIVE GROSS MARGIN IN DEALER COST. There are  " & Range("BQ1").Value & "   item(s) that have negative GM and are missing price confirmation.  Please confirm price by selecting YES in Column S."
Else


If Range("BS1").Value > 0 Then

MsgBox Range("BS1").Value & " - ITEM(S) WHOSE INCREASE TO DEALER COST IS GREATER THAN 5 PERCENT.  There are  " & Range("BS1").Value & "   item(s) that have a price increase to Dealer Cost greater than 5% and are missing price confirmation. Please confirm price by selecting YES in Column S."

Else



End If
End If
End If
End If

End Sub

I need the macro to move to the next condition if the previous condition is false rather than closing the workbook. Thanks in advance!!!

How to&Answers:

I believe you are missing the ElseIf component

Option Explicit

Private Sub Workbook_BeforeClose(Cancel As Boolean)

If Range("CA1").Value > 0 Then
    MsgBox Range("CA1").Value & " - ITEM(S) NEED LIST PRICE CORRECTION.  There are  " & Range("CA1").Value & "   item(s) that presently have DEALER COST > LIST PRICE.  Please fix pricing for these item(s) as indicated in Column AA."
ElseIf Range("BO1").Value > 0 Then
    MsgBox Range("BO1").Value & " - ITEM(S) - PRICE JUSTIFICATION MISSING. There are  " & Range("BO1").Value & "   item(s) that have a 0% price change to Dealer Cost or a negative price change.  These items are missing PLM price justification in the comments field. Please add your comments to Column T."
ElseIf Range("BQ1").Value > 0 Then
    MsgBox Range("BQ1").Value & "- ITEM(S) - NEGATIVE GROSS MARGIN IN DEALER COST. There are  " & Range("BQ1").Value & "   item(s) that have negative GM and are missing price confirmation.  Please confirm price by selecting YES in Column S."
ElseIf Range("BS1").Value > 0 Then
    MsgBox Range("BS1").Value & " - ITEM(S) WHOSE INCREASE TO DEALER COST IS GREATER THAN 5 PERCENT.  There are  " & Range("BS1").Value & "   item(s) that have a price increase to Dealer Cost greater than 5% and are missing price confirmation. Please confirm price by selecting YES in Column S."
End If

End Sub

Answer:

You can restructure the conditions with an inverted Select Case statement:

Cancel = True
Select Case True

    Case (Range("CA1").Value > 0)
        '...

    Case (Range("BO1").Value > 0)
        '...

    Case (Range("BQ1").Value > 0)
        '...

    Case (Range("BS1").Value > 0)
        '...

    Case True
        Cancel = False ' let it close

End Select

Or, un-nest the conditions and use ElseIf:

Cancel = True
If Range("CA1").Value > 0 Then
    '...
ElseIf Range("BO1").Value > 0 Then
    '...
ElseIf ...
    '...
Else
    Cancel = False
End If

Note that the Cancel parameter should be set to True before the procedure exits, if you mean to keep that workbook opened.