插入MessageBox时出现问题以取消关闭文件

时间:2011-05-17 16:40:51

标签: excel vba

我正在尝试创建一个提示,提醒用户在关闭文件之前导出该文件。我的代码如下所示:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim Response As Long

    Response = MsgBox("If changes have been made to this file without running the Monthly Claim Program, you MUST run the 'Export to Mastersheet' program.  If you have not yet exported the file, please press NO, and do so.  Otherwise, press YES to close this file", vbYesNo)
    If Response = vbNo Then
       Cancel = True
    Else

    End If
    Call Remove_Macros_Menu
End Sub

但是,无论我为Cancel功能选择True还是False,文件都会关闭。任何想法为什么会这样?

1 个答案:

答案 0 :(得分:0)

凯西,

ThisWorkbook模块中的代码是什么?它需要在那里,否则它将不会做任何事情。此外,假设Remove_Macros_Menu删除了此工作簿的自定义菜单,我认为您需要在Else子句中调用它的行,尤其是在导出代码的按钮位于该菜单中时。否则,即使他们没有按

,您也会删除菜单

请注意删除BeforeClose事件中的菜单存在缺陷,即如果工作簿未保存,用户将看到“保存,不保存,取消”菜单。如果他们选择取消,则取消关闭,但您的菜单仍然被删除。