无法将自动筛选的范围值转换为数组 – 只做第一个

只返回已过滤单元格的返回列表的第一个值。 否则工作,如果我不使用过滤的单元格:

Dim tmp, tmpfilter As Variant ' returns the list of cell values tmp = teamRosterSheet.Range("D2", teamRosterSheet.Range("D2").End(xlDown)).Value ' returns 3 Debug.Print teamRosterSheet.Range("D2", teamRosterSheet.Range("D2").End(xlDown)).Cells.SpecialCells(xlCellTypeVisible).Count ' but only returns the first cell value and not all three other cells in my example (filtering is working ok as I can see the 3 non-contiguous rows only been shown) tmpfilter = teamRosterSheet.Range("D2", teamRosterSheet.Range("D2").End(xlDown)).Cells.SpecialCells(xlCellTypeVisible).Value 

我究竟做错了什么?

一个很好的问题! 当您将AutoFiltered列拖到VBA数组中时,您必须循环访问SpecialCells。 例如:

演示

 Sub GrabData() Dim rGrab As Range, ary() Set rGrab = Range("B2:B13").Cells.SpecialCells(xlCellTypeVisible) ReDim ary(1 To rGrab.Count) i = 1 For Each r In rGrab ary(i) = r.Value i = i+1 Next r End Sub