在我的工作簿关闭之前,我已经编写了以下代码来重置任何验证和/或自动过滤:
Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Worksheets("Query").Cells.SpecialCells(xlCellTypeAllValidation).Clear
On Error Resume Next
Worksheets("Table").Cells.SpecialCells(xlCellTypeAllValidation).Clear
On Error Resume Next
Worksheets("Table").ShowAllData
On Error Resume Next
Worksheets("Query").Activate
ThisWorkbook.Save
End Sub
然而,'工作表("表")。ShowAllData'似乎只在即时窗口中工作。当我在表格"表格"上过滤表格后关闭程序并再次打开它,我得到
We found a problem with some content in "Book1.xlsm". Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes.
这一开始并没有发生,似乎没有原因。为什么过滤器重置在立即窗口中工作,而不是在我的子程序中?我该如何解决?
答案 0 :(得分:0)
永远不要使用On Error Resume Next
,否则你永远不会学到任何东西
您需要首先检查Autofilter
是否已开启,因为它是一个切换...
If ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
或
If ActiveSheet.FilterMode Then
ActiveSheet.AutoFilterMode = False
End If
答案 1 :(得分:0)
无需拥有这么多OERN On Error Resume Next
。只有一个人会这样做。
我试过这个
Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Worksheets("Query").Cells.SpecialCells(xlCellTypeAllValidation).Clear
Worksheets("Table").Cells.SpecialCells(xlCellTypeAllValidation).Clear
On Error GoTo 0
If Worksheets("Table").FilterMode Then Worksheets("Table").ShowAllData
Worksheets("Query").Activate
ThisWorkbook.Save
DoEvents
End Sub
我没有得到任何错误。试试吧。
我们发现了#34; Book1.xlsm"中的某些内容存在问题。你想让我们尽可能多地恢复吗?如果您信任此工作簿的来源,请单击“是”。
如果仍然出现错误,则不是因为这段代码。您需要查看有关已修复内容的报告。我感觉你的一个DataValidation
列表导致了这个问题。就像我说的,检查修复报告。如果报告说Datavalidation
已修复,那么您需要检查一下。