简体   繁体   English

错误信息 Run time error 1004 Autofilter method or range class failed

[英]Error message Run time error 1004 Autofilter method or range class failed

I am trying to create a filter on column C which displays all value except the value a90.我正在尝试在 C 列上创建一个过滤器,它显示除值 a90 之外的所有值。

在此处输入图片说明

I get error message:我收到错误消息:

Run time error 1004运行时错误 1004
Autofilter method of range class failed范围类的自动过滤方法失败

The error comes from this line:错误来自这一行:

ActiveWorkbook.ActiveSheet.Range("$A$4:$C$1300").AutoFilter Field:=3, Criteria1:="<>*a90*" _
        , Operator:=xlAnd
Sub classificationfilterwithoutvaluea90 ()
Range("C6").Select
Selection.AutoFilter
ActiveWorkbook.ActiveSheet.Range("$A$4:$C$1300").AutoFilter Field:=3, Criteria1:="<>*a90*" _
        , Operator:=xlAnd
End Sub

My data in column C are not part of a pivot table whereas my data in column A and B are part of a pivot table.我在 C 列中的数据不是数据透视表的一部分,而我在 A 列和 B 列中的数据是数据透视表的一部分。

I tested this out, and I think is what you want or at least it should help get what you want.我对此进行了测试,我认为这是您想要的,或者至少应该有助于获得您想要的。

Couple of things to note though prior.虽然之前需要注意几件事。 These lines are unnecessary:这些行是不必要的:

Range("C6").Select
    Selection.AutoFilter

First thing is that you should not be letting VBA implicitly decide which range in which worksheet in which workbook that you want to be using.首先,您不应该让 VBA 隐式决定要使用哪个工作簿的哪个工作表的哪个范围。 It seems, in this case, that you're trying to get the value of cell C6 for later use, so to do that I would do something like this.在这种情况下,您似乎正在尝试获取单元格 C6 的值以供以后使用,因此我会这样做。

Dim ws As Worksheet
Dim myVal As String

'pick whichever worksheet you want to work with, i'm just choosing at random here
Set ws = ThisWorkbook.Worksheets(1)
'assigns the value of cell C6 to myVal. CStr() is casting whatever value is in C6 to a string
myVal = CStr(ws.Cells(6,3))

As I said previously, you don't really want VBA to be deciding what these variables will be for you, so you should explicitly set them.正如我之前所说,您并不真的希望 VBA 决定这些变量对您来说是什么,因此您应该明确设置它们。

ActiveWorkbook.ActiveSheet.Range("$A$4:$C$1300")

Using the Active variables usually leads to shooting yourself in the foot.使用Active变量通常会导致用脚射击自己。 In this case, since you're working with 1 workbook, the ThisWorkbook variable is sufficient because - I think - that it is specifying the workbook that is executing the code, but you should also be specifying the worksheet you want to work with.在这种情况下,由于您正在使用 1 个工作簿,因此ThisWorkbook变量就足够了,因为 - 我认为 - 它指定了正在执行代码的工作簿,但您还应该指定要使用的工作表。

Here's the code that I tested:这是我测试的代码:

Option Explicit

Public Sub classificationfilterwithoutvaluea90()
    Dim thisws As Worksheet
    Dim filterArea As Range
    Const filterCrit As String = "<>a90"

    'idk which worksheet you want, so I just picked one
    Set thisws = ThisWorkbook.Worksheets(1)
    'This works as an alternative as well
    'Set thisws = ThisWorkbook.Worksheets("Sheet1")
    'setting the range to be filtered
    Set filterArea = thisws.Range("A4:C1300")

    filterArea.AutoFilter _
        Field:=1, _
        Criteria1:=filterCrit, _
        Operator:=xlFilterValues
End Sub

Hope it helps希望能帮助到你

暂无
暂无

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

相关问题 运行时错误1004 - 范围类的自动过滤方法失败 - Run-time error 1004 - Autofilter Method of Range Class Failed 运行时错误“1004”:范围类的自动过滤方法失败 - run-time error '1004': Autofilter method of range class failed 运行时错误&#39;1004-Range类的AutoFilter方法失败 - Run-Time error '1004 - AutoFilter method of Range class failed 运行时错误“1004”--范围类的自动过滤方法失败 - Run-time Error '1004'-- AutoFilter Method of Range Class Failed 录制的 VBA 宏 - 运行时错误 1004 范围类的自动过滤方法失败 - Recorded VBA macro - run time error 1004 autofilter method of range class failed Excel VBA 运行时错误 1004 范围 class 的自动筛选方法失败 - 出了什么问题? - Excel VBA Run Time Error 1004 AutoFilter method of Range class failed - what is wrong? VBA运行时错误&#39;1004&#39;。 Range类的AutoFilter方法失败 - VBA Runtime error '1004'. AutoFilter method of Range class failed 运行时错误:Range AutoFilter上的'1004' - Run-time error: '1004' on Range AutoFilter VBA:为什么自动过滤器在手动过滤器时不起作用? (运行时错误&#39;1004&#39;:对象&#39;Range&#39;的方法&#39;AutoFilter&#39;失败) - VBA: Why will Autofilter not work when manual filter does? (Run-time error '1004': Method 'AutoFilter' of object 'Range' failed) 运行时错误“ 1004”:范围类的VBA选择方法失败 - Run Time Error '1004': Select method of Range Class failed VBA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM