首页 > 解决方案 > 阻止 Excel 用户使用 X、最小化和最大化按钮关闭电子表格

问题描述

如果单元格 H40 不包含文本值,我需要一些帮助来阻止 Excel 中的电子表格关闭。

这是一个范围的宏,而不是单个单元格:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If Sheets("Daily Centre Inputs").Range("D6,F6,C8:C18,I6:I18,A22:K22,A29,A36,H36").Value = "" Then
        MsgBox "Incomplete fields. Please check your data ensuring any required cells are complete otherwise you will not be able to close or save the workbook"
        Cancel = True
    End If
End Sub

标签: excelvba

解决方案


这将在 H40 更改且为空时触发。您需要将其添加到您希望它触发的 VBA 工作表中。

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("H40")) Is Nothing Then
        If IsEmpty(Range("H40")) Then
            ' do something
        End If
    End If
End Sub

推荐阅读