简体   繁体   English

Excel VBA自动筛选器错误

[英]Excel VBA autofilter error

I am trying to make my work easier. 我正在努力使我的工作更轻松。 I have an worksheet with a pivottable which get its data from a ms sql database (over 100000 records, if that counts). 我有一个带有数据透视表的工作表,该数据表从ms sql数据库获取数据(超过100000条记录,如果算在内)。 I need to autofilter this data. 我需要自动过滤这些数据。 So I google for it. 所以我用谷歌搜索。

I have found this vba code: 我发现了这个VBA代码:

Private Sub Worksheet_Calculate()

Range("A11").CurrentRegion.AutoFilter field:=6, Criteria1:=Range("d5").Value

End Sub

I concluded that I can apply it to my case. 我得出的结论是,我可以将其应用于我的案件。

The range which I need to filter starts from A11, the filter should be applied to field no.6 by the value found in cell D5. 我需要过滤的范围从A11开始,该过滤器应通过单元格D5中的值应用于字段6。

At first it does the filtering, but then I get this error: 起初它会进行过滤,但随后出现此错误:

Method 'Autofilter' of object 'Range' failed 对象“范围”的方法“自动过滤”失败

I don't know what is wrong, excel gets stuck and nothing works 我不知道怎么了,excel卡住了,什么也没用

Maybe someone could give me a hint or something. 也许有人可以给我一些提示。

Thank you. 谢谢。

I would recommend trying a fully-defined range for the autofilter, such as: 我建议尝试使用自动过滤器的完全定义范围,例如:

Sub fdsa()
    Dim LR as Long 'LR is last row
    LR = Cells( Rows.Count, "D").End(xlUp).Row 'Chose Col D to sort as it's the filter range
    Range("A11:O" & LR).AutoFilter field:=6, Criteria1:=Range("D5").Value 'Arbitrarily chose O as the stop column in the range... adjust as necessary
End Sub

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

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