我在A列,1,2,3,4,5和A,B,C中有8个变量。
我的目标是过滤掉A,B,C并仅显示1-5。
我可以使用以下代码执行此操作:
My_Range.AutoFilter Field:=1, Criteria1:=Array("1", "2", "3","4","5"), Operator:=xlFilterValues
但代码的作用是过滤变量1到5并显示它们。
我不会做相反的事情,但是通过过滤出A,B,C并显示变量1到5来产生相同的结果
我试过这段代码:
My_Range.AutoFilter Field:=1, Criteria1:=Array("<>A", "<>B", "<>C"), Operator:=xlFilterValues
但它不起作用。
为什么我不能使用此代码?
它给出了这个错误:
运行时错误1004范围类的autofilter方法失败
我该如何执行此操作?
答案 0 :(得分:17)
我认为(来自实验 - MSDN在这里没有用),没有直接的方法可以做到这一点。将Criteria1
设置为Array
等同于使用下拉列表中的复选框 - 正如您所说,它只会根据与数组中某个项匹配的项过滤列表。
有趣的是,如果您在列表中有文字值"<>A"
和"<>B"
并对这些值进行过滤,那么宏录制器会出现
Range.AutoFilter Field:=1, Criteria1:="=<>A", Operator:=xlOr, Criteria2:="=<>B"
哪个有效。但是,如果你有文字值"<>C"
,并且你在录制宏时过滤所有三个(使用勾选框),宏录制器会精确复制你的代码,然后失败并出现错误。我想我称之为一个错误 - 你可以使用你可以用VBA做的过滤器来做过滤。
无论如何,回到你的问题。可以过滤不等于某些条件的值,但最多只能过滤两个对您无效的值:
Range("$A$1:$A$9").AutoFilter Field:=1, Criteria1:="<>A", Criteria2:="<>B", Operator:=xlAnd
根据确切的问题,有几种可行的解决方法:
=ISNUMBER(A2)
或=NOT(A2="A", A2="B", A2="C")
然后过滤TRUE
Criteria1:=">-65535"
(或低于您预期的合适数量),这将过滤掉非数字值 - 假设这是您想要的例如:
Public Sub hideABCRows(rangeToFilter As Range)
Dim oCurrentCell As Range
On Error GoTo errHandler
Application.ScreenUpdating = False
For Each oCurrentCell In rangeToFilter.Cells
If oCurrentCell.Value = "A" Or oCurrentCell.Value = "B" Or oCurrentCell.Value = "C" Then
oCurrentCell.EntireRow.Hidden = True
End If
Next oCurrentCell
Application.ScreenUpdating = True
Exit Sub
errHandler:
Application.ScreenUpdating = True
End Sub
答案 1 :(得分:0)
我没有在互联网上找到任何解决方案,所以我实施了一个。
然后
具有标准的自动过滤器代码iColNumber = 1
Dim aFilterValueArray() As Variant
Call ConstructFilterValueArray(aFilterValueArray, iColNumber, Array("A", "B", "C"))
ActiveSheet.range(sRange).AutoFilter Field:=iColNumber _
, Criteria1:=aFilterValueArray _
, Operator:=xlFilterValues
实际上,ConstructFilterValueArray()方法(不是函数)获取它在特定列中找到的所有不同值,并删除最后一个参数中存在的所有值。
此方法的VBA代码是
'************************************************************
'* ConstructFilterValueArray()
'************************************************************
Sub ConstructFilterValueArray(a() As Variant, iCol As Integer, aRemoveArray As Variant)
Dim aValue As New Collection
Call GetDistinctColumnValue(aValue, iCol)
Call RemoveValueList(aValue, aRemoveArray)
Call CollectionToArray(a, aValue)
End Sub
'************************************************************
'* GetDistinctColumnValue()
'************************************************************
Sub GetDistinctColumnValue(ByRef aValue As Collection, iCol As Integer)
Dim sValue As String
iEmptyValueCount = 0
iLastRow = ActiveSheet.UsedRange.Rows.Count
Dim oSheet: Set oSheet = Sheets("X")
Sheets("Data")
.range(Cells(1, iCol), Cells(iLastRow, iCol)) _
.AdvancedFilter Action:=xlFilterCopy _
, CopyToRange:=oSheet.range("A1") _
, Unique:=True
iRow = 2
Do While True
sValue = Trim(oSheet.Cells(iRow, 1))
If sValue = "" Then
If iEmptyValueCount > 0 Then
Exit Do
End If
iEmptyValueCount = iEmptyValueCount + 1
End If
aValue.Add sValue
iRow = iRow + 1
Loop
End Sub
'************************************************************
'* RemoveValueList()
'************************************************************
Sub RemoveValueList(ByRef aValue As Collection, aRemoveArray As Variant)
For i = LBound(aRemoveArray) To UBound(aRemoveArray)
sValue = aRemoveArray(i)
iMax = aValue.Count
For j = iMax To 0 Step -1
If aValue(j) = sValue Then
aValue.Remove (j)
Exit For
End If
Next j
Next i
End Sub
'************************************************************
'* CollectionToArray()
'************************************************************
Sub CollectionToArray(a() As Variant, c As Collection)
iSize = c.Count - 1
ReDim a(iSize)
For i = 0 To iSize
a(i) = c.Item(i + 1)
Next
End Sub
返回一个String数组肯定可以改进此代码,但在VBA中使用Array并不容易。
注意:此代码仅在您定义名为X的工作表时才有效,因为AdvancedFilter()中使用的CopyToRange参数需要Excel范围!
令人遗憾的是Microfsoft没有实现这个解决方案,只需添加一个新的枚举作为xlNotFilterValues! ...或xlRegexMatch!
答案 2 :(得分:0)
替代使用VBA的过滤功能
作为@schlebe最近回答的创新替代方案,我尝试使用 VBA 中集成的Filter
功能,允许过滤掉给定的搜索字符串将第三个参数设置为False。所有&#34;否定&#34; 搜索字符串(例如A,B,C)都在数组中定义。我将A列中的条件读入数据字段数组,并基本执行后续过滤(A - C)以过滤掉这些项目。
<强>代码强>
Sub FilterOut()
Dim ws As Worksheet
Dim rng As Range, i As Integer, n As Long, v As Variant
' 1) define strings to be filtered out in array
Dim a() ' declare as array
a = Array("A", "B", "C") ' << filter out values
' 2) define your sheetname and range (e.g. criteria in column A)
Set ws = ThisWorkbook.Worksheets("FilterOut")
n = ws.Range("A" & ws.Rows.Count).End(xlUp).row
Set rng = ws.Range("A2:A" & n)
' 3) hide complete range rows temporarily
rng.EntireRow.Hidden = True
' 4) set range to a variant 2-dim datafield array
v = rng
' 5) code array items by appending row numbers
For i = 1 To UBound(v): v(i, 1) = v(i, 1) & "#" & i + 1: Next i
' 6) transform to 1-dim array and FILTER OUT the first search string, e.g. "A"
v = Filter(Application.Transpose(Application.Index(v, 0, 1)), a(0), False, False)
' 7) filter out each subsequent search string, i.e. "B" and "C"
For i = 1 To UBound(a): v = Filter(v, a(i), False, False): Next i
' 8) get coded row numbers via split function and unhide valid rows
For i = LBound(v) To UBound(v)
ws.Range("A" & Split(v(i) & "#", "#")(1)).EntireRow.Hidden = False
Next i
End Sub
答案 3 :(得分:0)
使用AutoFilter的选项
Option Explicit
Public Sub FilterOutMultiple()
Dim ws As Worksheet, filterOut As Variant, toHide As Range
Set ws = ActiveSheet
If Application.WorksheetFunction.CountA(ws.Cells) = 0 Then Exit Sub 'Empty sheet
filterOut = Split("A B C D E F G")
Application.ScreenUpdating = False
With ws.UsedRange.Columns("A")
If ws.FilterMode Then .AutoFilter
.AutoFilter Field:=1, Criteria1:=filterOut, Operator:=xlFilterValues
With .SpecialCells(xlCellTypeVisible)
If .CountLarge > 1 Then Set toHide = .Cells 'Remember unwanted (A, B, and C)
End With
.AutoFilter
If Not toHide Is Nothing Then
toHide.Rows.Hidden = True 'Hide unwanted (A, B, and C)
.Cells(1).Rows.Hidden = False 'Unhide header
End If
End With
Application.ScreenUpdating = True
End Sub
答案 4 :(得分:0)
这里有一个使用在某个范围内写入的列表的选项,填充将要过滤的数组。信息将被删除,然后对列进行排序。
Sub Filter_Out_Values()
'Automation to remove some codes from the list
Dim ws, ws1 As Worksheet
Dim myArray() As Variant
Dim x, lastrow As Long
Dim cell As Range
Set ws = Worksheets("List")
Set ws1 = Worksheets(8)
lastrow = ws.Cells(Application.Rows.Count, 1).End(xlUp).Row
'Go through the list of codes to exclude
For Each cell In ws.Range("A2:A" & lastrow)
If cell.Offset(0, 2).Value = "X" Then 'If the Code is associated with "X"
ReDim Preserve myArray(x) 'Initiate array
myArray(x) = CStr(cell.Value) 'Populate the array with the code
x = x + 1 'Increase array capacity
ReDim Preserve myArray(x) 'Redim array
End If
Next cell
lastrow = ws1.Cells(Application.Rows.Count, 1).End(xlUp).Row
ws1.Range("C2:C" & lastrow).AutoFilter field:=3, Criteria1:=myArray, Operator:=xlFilterValues
ws1.Range("A2:Z" & lastrow).SpecialCells(xlCellTypeVisible).ClearContents
ws1.Range("A2:Z" & lastrow).AutoFilter field:=3
'Sort columns
lastrow = ws1.Cells(Application.Rows.Count, 1).End(xlUp).Row
'Sort with 2 criteria
With ws1.Range("A1:Z" & lastrow)
.Resize(lastrow).Sort _
key1:=ws1.Columns("B"), order1:=xlAscending, DataOption1:=xlSortNormal, _
key2:=ws1.Columns("D"), order1:=xlAscending, DataOption1:=xlSortNormal, _
Header:=xlYes, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod:=xlPinYin
End With
End Sub
答案 5 :(得分:0)
这对我有用:
这是两个字段/列(9和10)上的条件,它过滤第9列的值> 0的行和第10列的值4、7和8的行。lastrow
是上的行数数据部分。
ActiveSheet.Range("$A$1:$O$" & lastrow).AutoFilter Field:=9, Criteria1:=">0", Operator:=xlAnd
ActiveSheet.Range("$A$1:$O$" & lastrow).AutoFilter Field:=10, Criteria1:=Arr("4","7","8"), Operator:=xlFilterValues
答案 6 :(得分:0)
请检查这个以过滤出范围内的值。它有效
Selection.AutoFilter field:=33, Criteria1:="<>Array(IN1R,IN2R,INDA)", Operator:=xlFilterValues
其实上面的代码是行不通的。因此,每当活动单元格具有我正在搜索的值时,我都会给出一个循环来隐藏整行。
对于选择中的每个单元格 如果 cell.value = “IN1R” 或 cell.value = “INR2” 或 cell.value = “INDA” 那么
Else
Activecell.Entirerow.Hidden = True
End if
下一步
答案 7 :(得分:-3)
替换运算符:= xlOr与运算符:= xlAnd在您的条件之间。见下面修改后的脚本
myRange.AutoFilter字段:= 1,Criteria1:=“&lt;&gt; A”,运算符:= xlAnd,Criteria2:=“&lt;&gt; B”,运算符:= xlAnd,Criteria3:=“&lt;&gt; ; C“