excel 具有多个条件的“BeforeClose”函数

gmol1639  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(146)

如果单元格B34〉0J34 = 0,是否有方法阻止Excel关闭?
如果他们不填写B34,则不需要填写J34,工作簿可以关闭。
如果他们将数据输入B34,我们也需要J34中的信息。
大概是这样的:

Private Sub Workbook_BeforeClose(Cancel As Boolean)

    If Application.Sheets("Sheet1").Range("B34").Value > "" and _
           Application.Sheets("Sheet1").Range("B34").Value = "" Then
        Cancel = True
        MsgBox "Please fill in the total % in cell J34"
   End If

End Sub
vmjh9lq9

vmjh9lq91#

在ThisWorkbook对象中:

Option Explicit

Private Sub Workbook_BeforeClose(Cancel As Boolean)

    Dim wb As Workbook
    Dim ws As Worksheet
    
    Set wb = ThisWorkbook
    Set ws = wb.Sheets("Sheet1")
 
    If ws.Range("B34").Value <> "" And ws.Range("J34").Value = "" Then
        Cancel = True
        MsgBox "Please fill in the total % in cell J34"
    End If
 
End Sub

相关问题