简体   繁体   English

过滤xlCellTypeVisible后的Excel VBA范围

[英]Excel VBA range after filtering xlCellTypeVisible

What I want to accomplish: Open a workbook from a specific address, Filter the first column for value equal to 36 or 541 (I got this first part working), then check column 3 to see if a value of 2 exists and if it exists then filter out everything except for value 2 in column 3; 我要完成的工作:从特定地址打开一个工作簿,过滤第一列的值等于36或541(我使第一部分开始工作),然后检查第3列以查看是否存在2值以及是否存在然后滤除第3列中值2以外的所有内容; if value 2 does not exist in column 3 then skip. 如果第3列中不存在值2,则跳过。

I tried using SpecialCells(xlCellTypeVisible) to name the new range but I must be using it incorrectly because it is giving me a value of 2 that exists only in the old range where the data has not been filtered yet. 我尝试使用SpecialCells(xlCellTypeVisible)命名新范围,但我必须使用不正确,因为它给我的值2仅存在于尚未过滤数据的旧范围中。

Thanks for your time! 谢谢你的时间!

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

Something like this should work: 这样的事情应该起作用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM