简体   繁体   English

Excel VBA-循环未将自动过滤器添加到我的工作表

[英]Excel VBA - Loop not adding autofilters to my worksheets

I have some code that (should) loop through all my worksheets and add an autofilter, however for some reason they're not showing up. 我有一些代码(应该)遍历所有工作表并添加一个自动筛选器,但是由于某些原因它们没有显示。 When I turn on events, I can see that it is quickly being added then removed almost instantly. 当我打开事件时,可以看到它正在快速添加,然后几乎立即被删除。 I'm assuming that this is because of something written earlier in my code, but I have too much code before this to evaluate what is causing the issue... Is there something I can add to guarantee the filters are added? 我以为这是由于我的代码前面写的东西,但是在此之前我有太多代码来评估导致问题的原因...是否可以添加某些内容以确保添加了过滤器? Code: 码:

Dim wsfixer As Worksheet
    For Each wsfixer In ActiveWorkbook.Worksheets
With ActiveSheet
       .AutoFilterMode = False
       .Range("A:S").AutoFilter
End With
On Error Resume Next
   Next

If you want to process the code on each worksheet, change 如果要处理每个工作表上的代码,请更改

With ActiveSheet

to

With wsfixer

By using With ActiveSheet the code within the With block is being "shortcutted" to using the active sheet (eg .AutoFilterMode is treated as ActiveSheet.AutoFilterMode ). 通过使用With ActiveSheetWith块中的代码被“简化”为使用活动工作表(例如.AutoFilterMode被视为ActiveSheet.AutoFilterMode )。 So you are executing the same code over and over to the one active sheet. 因此,您要一遍又一遍地执行相同的代码。

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

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