简体   繁体   English

使用VBA清除Excel AutoFilter

[英]Clearing Excel AutoFilter using VBA

I am using this VBA code to refresh my entire workbook at specific time intervals. 我正在使用此VBA代码以特定的时间间隔刷新我的整个工作簿。 ( thanks to this thread ) 感谢这个帖子

As you can see, it is currently set to refresh every 60 minutes. 如您所见,它目前设置为每60分钟刷新一次。

Public RunWhen As Double
Public Const cRunIntervalMinutes = 60
Public Const cRunWhat = "Workbook_RefreshAll"

Sub StartTimer()
    RunWhen = Now + TimeSerial(0, cRunIntervalMinutes, 0)
    Application.OnTime earliesttime:=RunWhen, procedure:=cRunWhat, _
         schedule:=True
End Sub

Sub StopTimer()
   On Error Resume Next
   Application.OnTime earliesttime:=RunWhen, _
       procedure:=cRunWhat, schedule:=False
End Sub

Sub Workbook_RefreshAll()
    Application.CalculateFullRebuild
    ActiveWorkbook.RefreshAll
    Call StartTimer
End Sub

The code works great, however there's one thing I wish it could also do: 代码工作得很好,但有一件事我希望它也可以做:

  1. Any time the sheet is opened, and/or 任何时候打开纸张,和/或
  2. Any time this VBA code executes the sheet refresh process 任何时候此VBA代码执行工作表刷新过程

I would like the AutoFilter to be "Cleared" (not disabled or turned off) as in the screen grab below: 我希望AutoFilter被“清除”(未禁用或关闭),如下面的屏幕抓取:

AutoFilter Clear 自动筛选清除

Thanks in advance for any assistance. 在此先感谢您的任何帮助。

Cheers 干杯

This should do the trick. 这应该可以解决问题。 You may of course do it for one Sheet only, if you wish. 如果您愿意,您当然可以只为一张纸做。

Sub Workbook_RefreshAll()
Dim sh As Worksheet
    Application.CalculateFullRebuild
    ActiveWorkbook.RefreshAll
    For Each sh In ActiveWorkbook.Worksheets
        If Not (sh.AutoFilter Is Nothing) Then sh.AutoFilter.ShowAllData
    Next sh
    Call StartTimer
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM