防止closures工作簿

有这个macros的ABC.xls文件。 现在,当我按下Ctrl + M时,macros有一个被调用的子组件。 这个小组将打开一个打开文件对话框,用户可以select一个CSV文件。 所以基本上,这个macros用于处理和保存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 

当用户closures工作簿时,我必须testing一个像CSV每一行的条件都有一个终止string。 如果该终止string不存在,我应该popup一个消息框给用户,并防止closures工作簿。 所以我做了以下…

 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 

点击closures(右上angular的X标记,closuresworkboox,我没有closuresexcel。)button,我可以看到消息框,但workbooxclosures。 如果行未正确结束,我希望用户进行一些编辑。 请build议。

编辑:由于上述代码不起作用,我使用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.... ”消息框。 如果我单击消息框上的Cancelbutton,工作簿不closures。

有没有办法自定义消息框文本或button或隐藏这个Save消息框?

如果您必须监视除包含macros之外的工作簿的closures,则可以按如下方式从已启用macros的工作簿中截取应用程序级别的事件:

在ThisWorkbook后添加(或修改)这个代码:

 Option Explicit Private m_CloseHelper As CloseHelper Private Sub Workbook_Open() Set m_CloseHelper = New CloseHelper End Sub 

添加一个名为CloseHelper的新类模块:

 Option Explicit Private WithEvents m_App As Excel.Application Private Sub Class_Initialize() Set m_App = Excel.Application End Sub Private Sub Class_Terminate() Set m_App = Nothing End Sub Private Sub m_App_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean) 'Logic goes here, eg the code below will prevent the user from closing 'any workbook other than this one, for as long as this workbook is open. If Not Wb Is ThisWorkbook Then Cancel = True MsgBox "Hello from m_App_WorkbookBeforeClose" End If End Sub 

重要的关键字是WithEvents,原则是Excel应用程序引发的事件现在可以在CloseHelper类中进行编码。

您将在这里find更深入的文章: 应用程序事件

按右上angular的Xbutton退出Excel应用程序。 当应用程序closures时,您不应该期望工作簿保持打开状态。 你可以编写一个应用程序事件程序,防止Excel退出,但更好的方法可能是简单地摆脱使用该button的坏习惯。 查找另一种closures工作簿的方法。

Workbook_BeforeClose事件只适用于代码所在的工作簿。 它在您尝试closuresmacros工作簿并将其参数“取消”设置为“仅”时closuresExcel运行时取消closuresmacros工作簿时运行。 打开的csv文件是一个单独的工作簿,Excel试图单独closures它,生成一个关于保存csv文件的单独消息。

要自动防止csv文件被closures,您需要使用扩展性编程,以在Excel中打开时将Workbook_BeforeClose事件添加到csv文件。 显然你不能保存事件到位的文件,但你不需要。 坐在模块中的代码将在工作簿仍处于打开状态时运行,以防止在您的条件不满足时closures; 并且条件满足后允许成功的保存将以.csv格式擦除临时添加的代码。

http://www.cpearson.com/excel/vbe.aspx

您是否尝试closures所有加载项 (或在安全模式下运行Excel)?

我在问,因为这个问题可能是由于你的一个加载项包含一个应用级事件,如Excelosaurus的答案中所描述的。 它可能包含Application.EnableEvents = False甚至Cancel = False并干扰您的代码。

编辑:这将是特别难以检测与加载项保护,因为代码将 Workbook_BeforeClose 运行即使您正在逐行debugging使用F8,您将无法知道它在做什么。

您可以在工作簿macros中使用此代码

 Private Sub Workbook_BeforeClose(Cancel As Boolean) ActiveWorkbook.Save Workbooks.Open Application.ActiveWorkbook.FullName End Sub 

正如你所看到的,它基本上保存并在closures时打开工作簿