简体   繁体   English

VBA:为什么自动过滤器在手动过滤器时不起作用? (运行时错误'1004':对象'Range'的方法'AutoFilter'失败)

[英]VBA: Why will Autofilter not work when manual filter does? (Run-time error '1004': Method 'AutoFilter' of object 'Range' failed)

Getting a Run time error for the following code. 获取以下代码的运行时错误。

Working with a data dump that has data in rows 1000, etc for some columns. 对于某些列,使用包含行1000等数据的数据转储。 From what I read on other forums, autofilter won't work if there is no values "near" the cell. 从我在其他论坛上看到的内容来看,如果单元格附近没有值,则autofilter将无法工作。 However, when I put in test value closer to the filter, it still won't work. 但是,当我将测试值放在更接近过滤器的位置时,它仍然无法工作。 Manual filter is working on this same range. 手动过滤器正在同一范围内工作。

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

It's not quite clear what you are trying to do and how it does not work. 目前还不是很清楚你要做什么以及它是如何工作的。

Your code 你的代码

  • adds a column to the left 在左侧添加一列
  • types a pipe to all the rows below the selected cell (row) 键入选定单元格(行)下方所有行的管道
  • puts on the filter function starting from the selected row and including not the two rightmost columns (the added and -1 makes -2) of the original data 从所选行开始放置过滤器函数并且不包括原始数据的两个最右边的列(添加的和-1使-2)
  • uses Freeze Pane to keep the selected row visible while scrolling 使用冻结窗格在滚动时保持所选行可见

IF your idea were to filter out the rows you marked with pipes, you'd need a title for the column AND IF the column titles would be on row 1 my modified version would be like this: 如果您的想法是过滤掉用管道标记的行,则需要列的标题,如果列标题位于第1行,我的修改版本将如下所示:

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

But clearly I have too many ifs to answer to your question. 但显然我有太多的ifs来回答你的问题。 :-) :-)

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

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