【问题标题】:Excel VBA range after filtering xlCellTypeVisible过滤 xlCellTypeVisible 后的 Excel VBA 范围
【发布时间】:2015-06-17 17:31:13
【问题描述】:

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

我尝试使用 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

【问题讨论】:

  • 您不能使用语法newrange(i, 3).Value 访问多区域范围内的值。
  • 即使使用该语法它仍然可以运行。即使我输入 Cells(i, 3).Value 而不是 newrange,我也会得到相同的值。
  • 它会运行(即它不会抛出错误):它只是不会给你正确的答案......
  • 哦,好的。所以我修改它说“细胞”而不是“新范围”。过滤器后是否无法检查列中的值?谢谢。

标签: vba excel filter range


【解决方案1】:

这样的事情应该可以工作:

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

【讨论】:

  • 谢谢蒂姆!像魅力一样工作!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-28
  • 1970-01-01
  • 2020-02-29
相关资源
最近更新 更多