如果出现MsgBox,请停止宏

问题描述 投票:0回答:3

我有一个执行一些验证的宏。我简化了下面的代码以进行说明。

  • 如果所有验证都成功,那么我希望它运行“ ActiveWorkbook.SaveAs”行。
  • 如果必须弹出MsgBox,则我不想运行“ ActiveWorkbook.SaveAs”行

我如何调整代码以使之成为可能?谢谢您的帮助!

Sub blank()

'Validations
If Cells(10, 4).Value = "" Then
MsgBox "Please fill in Invoice Date"
End If

If Cells(11, 4).Value = "" Then
MsgBox "Please fill in Invoice Number"
End If

'Save As
ActiveWorkbook.SaveAs filename:="Lease Admin Processing Input Form"

End Sub
excel vba excel-vba
3个回答
1
投票

您可以这样做:

Sub blank()

    If Cells(10, 4).Value = "" Or Cells(11, 4).Value = "" Then
        MsgBox "Please fill in both Invoice Number and Invoice Date"
    Else
        ActiveWorkbook.SaveAs filename:="Lease Admin Processing Input Form"
    End If

End Sub

0
投票

只需检查两个验证是否都正确。如果它们都成立,则运行“另存为”部分,否则将跳过另存为:)

Sub blank()

'Validations
If Cells(10, 4).Value = "" Then
MsgBox "Please fill in Invoice Date"
End If

If Cells(11, 4).Value = "" Then
MsgBox "Please fill in Invoice Number"
End If

If Cells(10, 4).Value = "" And Cells(11, 4).Value = "" Then
'Save As
ActiveWorkbook.SaveAs filename:="Lease Admin Processing Input Form"
End If
End Sub

0
投票

也许像这样

Sub blank()

Dim bolChecker as boolean

bolChecker = false

'Validations
If Cells(10, 4).Value = "" Then
MsgBox "Please fill in Invoice Date"
bolChecker = true
End If

If Cells(11, 4).Value = "" Then
MsgBox "Please fill in Invoice Number"
bolChecker = true
End If

'Save As
if bolChecker = false then ActiveWorkbook.SaveAs filename:="Lease Admin Processing Input Form"

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.