我找到了这个代码,它完美无缺。但是当我点击关闭按钮时,对话框显示“你是否想保存更改”,如果我选择是,则出现错误,我在单元格A中没有值。然后我的文件自动关闭。
如何防止这种情况,并留在文档中?
我的代码是:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If ActiveSheet.Range("A1").Value = "" Then
Cancel = True
Response = MsgBox("Please enter a value in A1", vbCritical, "Error!")
End If
End Sub
答案 0 :(得分:0)
您是否尝试过使用BeforeClose而不是BeforeSave?
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If ActiveSheet.Range("A1").Value = "" Then
Cancel = True
Response = MsgBox("Please enter a value in A1", vbCritical, "Error!")
End If
End Sub
此致