有ABC.xls
文件有宏。现在,当我按Ctrl + M
时,该宏有一个被调用的子。 这个小组将打开一个打开文件对话框,用户可以在其中选择一个CSV
文件。所以基本上,这个宏用于处理和保存CSV
文件。 下面是按Ctrl + M
调用的代码。防止关闭工作簿
Sub runProperChangeSubroutine() ' Assigned to shortcut key CTRL + M.
file_name = ActiveWorkbook.Name ' Gets the file name.
Application.Run file_name & "!ChangeSub"
End Sub
当用户关闭工作簿,我要测试等每行中的CSV
具有终止字符串的条件。如果该终止字符串不是 ,我应该弹出一个消息框给用户并阻止关闭工作簿。 所以我做了以下...
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim improperRowNumber As Integer
Dim BlnEventState As Boolean
improperRowNumber = returnImproperRow()
BlnEventState = Application.EnableEvents
Application.EnableEvents = True
If improperRowNumber <> -1 Then
Cancel = True
MsgBox "The row number " & improperRowNumber & " is not ending with '@/#'. Please correct the row before closing."
Else
Call saveCSV
End If
Application.EnableEvents = BlnEventState
End Sub
在接近(在右上角的X标志,workboox。我不关闭Excel中。关闭)按钮的点击,我能够看到消息但工作台关闭。如果行没有正确结束,我希望用户进行一些编辑。 请建议。
被修改: 作为上述代码不工作,我消息框之后使用ThisWorkbook.Saved = False
如下所示...
If improperRowNumber <> -1 Then
MsgBox "The row number " & improperRowNumber & " is not ending with '@/#'. Please correct the row before closing."
Application.DisplayAlerts = False
ThisWorkbook.Saved = False
Cancel = False
Else
现在它显示“Do you want to save the changes....
”消息框。如果我在消息框上单击Cancel
按钮,工作簿未关闭。
是否有自定义消息框文本或按钮或隐藏此消息框的方法?
你不应该能够与取消关闭该工作簿=真在Workbook_BeforeClose中。可能你在soubroutine improperRowNumber中有类似EnableEvents = False的东西? – MarcinSzaleniec
不,EnableEvents语句不存在于improperRowNumber子中。有没有其他的可能性? – NJMR
@MarcinSzaleniec禁用了应用程序事件,msgbox不会显示出来......但OP的处理程序显然正在运行。 –