1
我已经编写了一个宏,用于搜索工作簿并将自动筛选器应用于任何具有名为“代码”列的列表对象。但是,当我应用过滤器时,它不会过滤掉空白行。任何想法如何我可以过滤掉这些?如何在使用Excel VBA自动筛选器时删除空白行
这里是施加在过滤器的代码:
Public Sub ApplyFilter(filter As Variant)
Dim wb As Workbook
Dim ws As Worksheet
Dim lo As ListObject
Set wb = ActiveWorkbook
' Loop through each sheet in the workbook
For Each ws In wb.Sheets
' Find any listobjects within the sheet
For Each lo In ws.ListObjects
Dim r As Integer
' Find the column named Code and filter on this column
r = lo.Range.Rows(1).Find("Code").Column
' Clear any existing filter
lo.Range.AutoFilter Field:=r
' If the filter code is not "All Categories", 999, apply the filter
If filter(0) <> 999 Then
lo.Range.AutoFilter Field:=r, Criteria1:=filter, Operator:=xlFilterValues
End If
Next
Next
End Sub
了在传递的过滤器是一个阵列,其可能只是有一个标准,或许多。我也曾尝试添加criteria2:=“”,但这并没有改变任何东西。
让我知道你是否有任何想法。谢谢!
下面是其他相关的代码:
Public Sub FilterInvoice(filter As Range)
Me.ApplyFilter Me.BuildFilter(filter)
End Sub
Public Function BuildFilter(filter As Range) As Variant
Dim r As Range
Dim arFilter() As String
' Get the cell of the current category
Set r = Range("Categories").Find(filter.Value)
' Set the initial filter value to the category id
ReDim Preserve arFilter(1)
arFilter(0) = r.Offset(0, -1).Value
' Find any child categories, add child id's to filter array
For c = 1 To Application.CountIf(Range("Categories").Columns(3), arFilter(0))
Dim PrevChild As Range
' Expand the filter array
ReDim Preserve arFilter(c + 1)
If c = 1 Then
Set PrevChild = Range("Categories").Columns(3).Find(arFilter(0))
Else
' If it is not the first time through the loop, look for the next child after PrevChild
Set PrevChild = Range("Categories").Columns(3).Find(arFilter(0), PrevChild)
End If
' Offset the found child to get its code, add it to the filter array
arFilter(c) = PrevChild.Offset(, -2)
Next
' Add "<>" and "<900" to the criteria list to hide blank rows
'ReDim Preserve arFilter(UBound(arFilter) + 2)
'arFilter(UBound(arFilter) - 1) = "<>"
'arFilter(UBound(arFilter)) = "<900"
'Return the filter array
BuildFilter = arFilter
End Function
你介意扩大?我需要的标准之一是我的数组包含我筛选的代码。我尝试使用数组作为条件之一,“<>”作为条件2,它没有给出任何结果。如果我将操作符设置为xlFilterValues,它会给出原始结果(空白行)。出于好奇,“<>”标准是什么意思? – JoshPeltier
请参阅我的编辑,“<>”表示“不为空” – Reafidy
如果您需要更多帮助,请向我展示填充您正在使用的实际过滤器的代码,并调用applyfilter子例程。 – Reafidy