簡體   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)

獲取以下代碼的運行時錯誤。

對於某些列,使用包含行1000等數據的數據轉儲。 從我在其他論壇上看到的內容來看,如果單元格附近沒有值,則autofilter將無法工作。 但是,當我將測試值放在更接近過濾器的位置時,它仍然無法工作。 手動過濾器正在同一范圍內工作。

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

目前還不是很清楚你要做什么以及它是如何工作的。

你的代碼

  • 在左側添加一列
  • 鍵入選定單元格(行)下方所有行的管道
  • 從所選行開始放置過濾器函數並且不包括原始數據的兩個最右邊的列(添加的和-1使-2)
  • 使用凍結窗格在滾動時保持所選行可見

如果您的想法是過濾掉用管道標記的行,則需要列的標題,如果列標題位於第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

但顯然我有太多的ifs來回答你的問題。 :-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM