我现在已经搜索了一个小时了,我一定不能使用正确的条款,因为我似乎无法找到任何符合我认为我需要做的事情。
我有一个小数据集,~100行x 9列。
我已经将它过滤了两个cols,结果数据集减少到8个记录。但是,我需要根据这8条记录再过滤一列。如何遍历该列的Criteria1数组? (Col A)
我想逐步浏览每个元素并对其进行测试......如果它通过,请保留它,如果没有...将其删除。但是,我似乎无法弄清楚如何逐步完成现有的Criteria1数组值。
我也想过,也许我可以使用For循环并逐步执行过滤后的行,但它会逐步遍历所有数据行,而不是过滤掉的数据行......
index = 1
'Just picked a range that started in the data area and would be past the number of items I need to go through
For i = 6 To 30 'First row # of filtered data is 13
If Cells(i, "A") = "" Then
'Blank Line
Exit For
Else
aTest(index) = Cells(i, "A")
Debug.Print aTest(index) 'Steps through each row of the unfiltered data, not filtered
index = index + 1
End If
Next i
基本上,这是我需要经历的值数组,并删除以90结尾的值。
ActiveSheet.Range("$A$5:$M$108").AutoFilter Field:=1, _
Criteria1:=Array("31510", "31605", "31607", "31608", "31690", "81603", _
"81604", "81690"), Operator:=xlFilterValues
需要有人指出我正确的方向......
答案 0 :(得分:1)
你能放手一搏吗?代码执行以下操作:
Criteria1
的候选项Right(val, 2) <> "90"
并存储传入数组的值Criteria1
参数HTH
Option Explicit
Sub Test()
Dim ws As Worksheet
Dim rngFilter As Range
Dim rngVisibleData As Range
Dim rngCell As Range
Dim varCriteria() As String
Dim lngCounter As Long
'quit if no filter
Set ws = ThisWorkbook.Worksheets("Sheet1")
If Not ws.AutoFilterMode Then
Exit Sub
End If
'get visibile cells in column A as candidates for filter
Set rngVisibleData = ws.Range("A2", ws.Range("A2").End(xlDown)).SpecialCells(xlCellTypeVisible)
'process candidates
lngCounter = 0
For Each rngCell In rngVisibleData
If Right(CStr(rngCell.Value), 2) <> "90" Then
lngCounter = lngCounter + 1
ReDim Preserve varCriteria(1 To lngCounter)
varCriteria(lngCounter) = CStr(rngCell.Value)
Debug.Print rngCell.Value
End If
Next rngCell
'add new filter with processed candidates
Set rngFilter = Range("A1").CurrentRegion
rngFilter.AutoFilter Field:=1, Criteria1:=varCriteria, Operator:=xlFilterValues
End Sub