VBA从Excel 2007中的filter函数获取值

在Excel 2007中使用“版本”菜单中的“筛选”function时,标题单元格右下angular显示小箭头。 当点击一个,popup列的每个不同的值列表,select它们的选项。

过滤示例

你如何获得价值并通过VBA循环?

我试过这个:

Dim Filter As Range For Each Filter In Range(Cells(2, 1), Cells(2, 1).End(xlDown)).SpecialCells(xlCellTypeVisible).Cells MsgBox (Filter.value) Next Filter 

但它不起作用(它循环通过列的所有单元格)。 也许是因为在运行macros时箭头没有被“点击”。 我已经在一篇文章中find了这个For Each循环来讨论Excel 2002。

[编辑]

以下不是我正在寻找的解决scheme,因为执行本地Excel方法需要更多的时间,但这是一个可以接受的解决方法。

 Dim values As New Collection Dim RowCount As Long RowCount = Cells(Rows.Count, "A").End(xlUp).Row Dim IsUnique As Boolean For i = 2 To RowCount IsUnique = True For Each value In values If value = Range("A" & i).value Then IsUnique = False End If Next value If IsUnique Then values.Add Range("A" & i).value End If Next i 

Find返回一个Range对象,该对象表示find该信息的第一个单元格。

您可以使用FindNext和FindPrevious方法来重复search

此示例查找工作表1中包含值2并将其更改为5的所有单元格范围A1:A500。

 With Worksheets(1).Range("a1:a500") Set c = .Find(2, lookin:=xlValues) If Not c Is Nothing Then firstAddress = c.Address Do c.Value = 5 Set c = .FindNext(c) Loop While Not c Is Nothing And c.Address <> firstAddress End If End With