【问题标题】:VBA Autofilter Excel 2013?VBA自动过滤Excel 2013?
【发布时间】:2015-07-30 17:21:49
【问题描述】:

我一直在使用这样的行:

Cells.AutoFilter 11, "0"

暂时将第 11 列自动过滤为“0”。

我最近更新到 Microsoft Office 2013,现在我收到了 AutoFilter method of Range class failed 这一行的运行时错误。这是与 Office 2013 的兼容性问题还是其他问题?

编辑:我应该澄清一下,我没有收到我已经运行的程序的错误,而是我以前使用过但现在不适合我的行。

EDIT2:

代码:

Dim firstRow As Integer
Dim lastRow As Integer
Dim firstCol As Integer
Dim lastCol As Integer
Dim allRange As Range
Dim vRange As Range
Dim bRange As Range
Dim commentsCol As Integer
Dim commentsColRng As Range
Dim fieldNameCol As Integer
Dim userCol As Integer

If Cells(2, 1) <> "" Then

    firstCol = 1
    lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
    firstRow = 2
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row

    commentsCol = Rows(1).find("Comments").Column '11
    fieldNameCol = Rows(1).find("Field Name").Column '8
    userCol = Rows(1).find("User").Column '4

    Set allRange = Range(Cells(firstRow, firstCol), Cells(lastRow, lastCol))
    Set commentsColRng = Range(Cells(firstRow, commentsCol), Cells(lastRow, commentsCol))

    Cells.AutoFilter Field:=1, Criteria1:="PF"
    Cells.AutoFilter commentsCol, "0", xlFilterValues
    Cells.AutoFilter Field:=2, Criteria1:="0", Operator:=xlFilterValues

End If

我有三个自动过滤器,因为我尝试了多种不同的方式。

【问题讨论】:

  • 嗯,这很奇怪。带有Same 代码的Same 文件是否在Excel 2010 中工作?
  • 作为一个仅供参考,我刚刚提取了 Excel 2013 的副本并使用了诸如您的代码进行测试,它运行良好。您确定代码在Activesheet 上运行不是问题(隐含因为代码没有指定工作表),并且您实际上需要过滤不同的工作表吗?你确定第 11 列有数据吗?
  • @SiddharthRout 我相信我们最初使用的是 Office 2003,所以我不知道 2010 年的情况。我之前编写的程序中也有类似的行。 Cells.AutoFilter Field:=2, Criteria1:="0", Operator:=xlFilterValues
  • @user3561813 我会尝试指定一个工作表,但我知道第 11 列有数据

标签: vba excel autofilter office-2013


【解决方案1】:

第 11 列中可能没有任何内容。

例如:

     A         B
---------------------
1  field1   field2
2  a             0 
3  b             1
4  c             2
5  d            23

Sub Test()
    ' this will succeed because column 2 (B) has something
    Cells.AutoFilter 2, "0"

    ' this will fail because column 3 (C) has nothing to filter on
    ' error message will be AutoFilter method of Range class failed
    Cells.AutoFilter 3, "0"
End Sub

【讨论】:

  • 我知道第 11 列的所有数据都有数据。前一个宏中的Cells.AutoFilter Field:=2, Criteria1:="0", Operator:=xlFilterValues 行对其程序运行良好,但此宏的行Cells.AutoFilter 11, "0", xlFilterValues 却不行。
  • 你能把 Excel 表格上传到某个地方吗?如果我们看看这个,我们可能会提供更好的帮助。
  • 更新了原帖
  • 我从屏幕截图中创建了相同的数据集,并以零问题运行了您的代码。代码运行良好。
  • 您使用的是 Excel 2013?
猜你喜欢
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-30
相关资源
最近更新 更多