VBA:为什么手动filter会自动filter不工作? (运行时错误'1004':对象'Range'的方法'AutoFilter'失败)

为下面的代码获取运行时错误。

处理某些列中具有行1000等数据的数据转储。 从我在其他论坛读到的内容来看,如果单元格附近没有任何值,自动filter将无法工作。 但是,当我把testing值放在靠近滤波器的位置时,它仍然不起作用。 手动filter正在这个范围内工作。

CURrow = ActiveCell.Row LASTrow = ActiveCell.SpecialCells(xlLastCell).Row LASTcol = ActiveCell.SpecialCells(xlLastCell).Column If LASTrow > CURrow Then Columns("A").Insert Shift:=xlToRight Range(Cells(CURrow + 1, 1), Cells(LASTrow, 1)).Value = "|" Columns(1).HorizontalAlignment = xlCenter Range(Cells(CURrow, 1), Cells(CURrow, LASTcol - 1)).AutoFilter Rows(CURrow + 1).Select ActiveWindow.FreezePanes = True End If 

目前还不清楚你想要做什么以及如何工作。

你的代码

  • 在左侧添加一列
  • 在所选单元格(行)下面的所有行中键入一个pipe道
  • 从所选行开始,并且不包括原始数据的两个最右边的列(添加的和-1使得-2)
  • 使用冻结窗格在滚动时保持选定的行可见

如果你的想法是过滤出你用pipe道标记的行,你需要一个标题的列和如果列标题将在第一行我的修改版本会是这样的:

 Sub MarkForFilterFromHereBelow() CURrow = ActiveCell.Row LASTrow = ActiveCell.SpecialCells(xlLastCell).Row LASTcol = ActiveCell.SpecialCells(xlLastCell).Column If LASTrow > CURrow Then Columns("A").Insert Shift:=xlToRight Range(Cells(CURrow + 1, 1), Cells(LASTrow, 1)).Value = "|" Columns(1).HorizontalAlignment = xlCenter Cells(1, 1).Value = "FilterColumn" 'This is the column title Range(Cells(1, 1), Cells(LASTrow, LASTcol + 1)).AutoFilter 'Here you can add a row to filter the rows marked with |? Rows(2).Select ActiveWindow.FreezePanes = True End If End Sub 

但显然我有太多的答案来回答你的问题。 🙂