只有在工作簿中执行某些操作时,如何防止Excel保存更改对话框

我有一个macros,可以启用/禁用根据我的自定义function区选项卡上的切换button

切换

切换macros运行时,为所选单元格创build注释,并在该注释中以及状态栏上显示该单元格内容的字符和字数。 我只保留了当前单元格的注释,并且一旦select了另一个单元格就删除它,使资源更加友好

细胞统计

在用户完成检查他们希望检查其统计信息的任何单元格之后,可以通过closures该button来禁用该function,在这些点上评论被再次删除。 这一切都完美,按计划。 但是,这是我遇到问题的地方。

我的代码:

这个工作簿模块:

'Used an instance of app instead of application because this will be implemented inside/from an add-in) Private WithEvents App As Application Private Sub App_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) If Not buttonToggled Then Exit Sub 'Set from other sub using global declared variable 'Use target(1,1) because you will do something based on the contents of a 'single cell -- the first cell in the range Sh.UsedRange.ClearComments If Intersect(Target(1, 1), Sh.UsedRange) Is Nothing Then Exit Sub '------> ' carry on from here Dim charcount As Integer: charcount = Len(Target(1, 1)) Dim wdcount As Integer: wdcount = UBound(Split(Target(1, 1).Value, " "), 1) + 1 Application.StatusBar = "Character Count: " & charcount & " | Word Count: " & wdcount Target(1, 1).AddComment "Character Count: " & charcount & vbNewLine & "Word Count: " & wdcount Target(1, 1).Comment.Shape.TextFrame.AutoSize = True End Sub 

常规模块:

 Public buttonToggled As Boolean Sub ToggleTest(control As IRibbonControl, pressed As Boolean) If pressed Then buttonToggled = True Else buttonToggled = False ActiveSheet.UsedRange.ClearComments Application.StatusBar = False End If End Sub 

我的问题:

如果用户使用此function,即使只有一个单元格(因为注释被创build和删除),他们会提示保存更改? 即使他们没有做任何其他更改,也可以在closures文件时select对话框

我的问题:

  1. 有没有办法不显示保存更改? 对话框只有当被“改变”的唯一的东西是由macros自动添加和删除的评论?

    • 我需要对话框来显示其他更改, 随着注释的自动添加和删除,以及用户随时进行更改(并且不启用macros)
  2. 如果这是不可能的,你会build议我做一个解决方法/调整?

– 更新了在答复中包含方法的实现

感谢下面提供的答案,我已经能够实现我的目标。

这个工作簿模块:

 Private WithEvents App As Application Private Sub App_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean) saveStatus = ActiveWorkbook.Saved ActiveSheet.UsedRange.ClearComments ActiveWorkbook.Saved = saveStatus End Sub Private Sub App_WorkbookBeforeSave(ByVal Wb As Workbook, ByVal SaveAsUI As Boolean, Cancel As Boolean) saveStatus = ActiveWorkbook.Saved ActiveSheet.UsedRange.ClearComments ActiveWorkbook.Saved = saveStatus End Sub Private Sub Workbook_Open() 'Application.ReferenceStyle = xlA1 Set App = Application End Sub Private Sub App_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) If Not buttonToggled Then Exit Sub 'Set from OnHoverCellLength sub using global declared variable 'Use target(1,1) because you will do something based on the contents of a 'single cell -- the first cell in the range saveStatus = ActiveWorkbook.Saved Sh.UsedRange.ClearComments If Intersect(Target(1, 1), Sh.UsedRange) Is Nothing Then Exit Sub '------> ' carry on from here Dim charcount As Integer: charcount = Len(Target(1, 1)) Dim wdcount As Integer: wdcount = UBound(Split(Target(1, 1).Value, " "), 1) + 1 Application.StatusBar = "Character Count: " & charcount & " | Word Count: " & wdcount Target(1, 1).AddComment "Character Count: " & charcount & vbNewLine & "Word Count: " & wdcount Target(1, 1).Comment.Shape.TextFrame.AutoSize = True ActiveWorkbook.Saved = saveStatus End Sub 

常规模块:

 Public buttonToggled As Boolean, saveStatus As Boolean Sub ToggleTest(control As IRibbonControl, pressed As Boolean) If pressed Then buttonToggled = True Else buttonToggled = False ActiveSheet.UsedRange.ClearComments Application.StatusBar = False ActiveWorkbook.Saved = saveStatus End If End Sub 

以下不是妥协解决scheme或“你能做的最好的” 它保留了工作簿的状态,就好像您的评论业务从未发生过一样。

  1. 创build一个模块级别的布尔variables,这个布尔variables对你的例程是可见的,这些例程可以打开和closuresfunction,创build/删除注释; 即使执行例程之后,该variables的值也必须保留。

  2. 在创build注释之前,将<Workbook Object>.Saved 。的值保存到布尔variables中。 删除注释后,将<Workbook Object>.Saved更新为您的布尔variables中的值。

如果您在打开/closuresmacros时对工作簿进行了任何更改,则可以使用相同的策略。 您不必担心任何时候<Workbook Object>.Saved