“需要先关闭”才能在多种条件下工作

时间:2019-04-23 17:48:39

标签: excel vba

我需要向BeforeClose事件中添加一些代码,以便在用户可以将工作表还给我之前,使用所需的缺少/不完整字段的数量来更新用户。问题是如果其中一个条件为假,则工作簿将关闭而不是继续进行下一个条件。

我已经研究了这个问题,但是在带有多个if条件的BeforeClose事件中,我找不到任何示例。

这是我到目前为止所拥有的:

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

如果前一个条件为false,则需要宏将其移至下一个条件,而不是关闭工作簿。在此先感谢!

2 个答案:

答案 0 :(得分:1)

我相信您缺少ElseIf组件

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

答案 1 :(得分:1)

您可以使用倒转的Select Case语句来重组条件:

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

或者,取消嵌套条件并使用ElseIf

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

请注意,如果您要保持工作簿处于打开状态,则应在过程退出之前将Cancel参数设置为True