【问题标题】:"BeforeClose" function with multiple conditions具有多个条件的“BeforeClose”函数
【发布时间】:2022-11-14 15:20:47
【问题描述】:

如果单元格 B34 > 0,有没有办法防止 Excel 关闭J34 = 0?

如果他们不理会 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

【问题讨论】:

    标签: excel vba conditional-statements


    【解决方案1】:

    在 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多