过滤xlCellTypeVisible后的Excel VBA范围

我想完成的工作:从一个特定的地址打开一个工作簿,过滤第一列的值等于36或541(我得到这第一部分工作),然后检查列3,看看是否存在2值,如果它存在然后过滤除列3中的值2以外的所有内容; 如果值2在列3中不存在,则跳过。

我尝试使用SpecialCells(xlCellTypeVisible)来命名新的范围,但我必须不正确地使用它,因为它给我一个只存在于数据尚未过滤的旧范围内的值2。

谢谢你的时间!

Sub filters() Dim wb As Workbook Dim nwb As Workbook Set wb = ThisWorkbook Set nwb = Workbooks.Open("ADDRESS.FILE.xlsx") With ActiveSheet .AutoFilterMode = False .Range("$A$1:$AD$5000").AutoFilter Field:=1, Criteria1:="=36", Operator:=xlOr, Criteria2:="=541" '.Range("$A$1:$AD$5000").AutoFilter Field:=3, Criteria1:="2" End With Dim newrange As Range Set newrange = ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible) Dim i As Integer, intValueToFind As Integer intValueToFind = 2 For i = 1 To 5000 ' Revise the 5000 to include all of your values If newrange(i, 3).Value = intValueToFind Then MsgBox ("Found value on row " & i) Exit Sub End If Next i ' This MsgBox will only show if the loop completes with no success MsgBox ("Value not found in the range!") End Sub 

像这样的东西应该工作:

 Dim newrange As Range, rw as range Set newrange = ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible) Dim intValueToFind As Integer intValueToFind = 2 For Each rw in newrange.Rows If rw.cells(3).Value = intValueToFind Then MsgBox ("Found value on row " & rw.cells(1).Row) Exit Sub End If Next rw