SpecialCells(12).Value 在第一个隐藏行之后停止

SpecialCells(12).Value stops after first hidden row

我目前正在尝试将过滤后的列复制到数组中以填充 Powerpoint 演示文稿中的组合框。 我用来执行此操作的代码行是:

ar = tbl.ListColumns(ColNumber).Range.SpecialCells(12).Value

其中“ar”是目标数组,“tbl”是源数组table,“ColNumber”是我要复制的列数。

我尝试复制的过滤列有大约 180 条记录,但我注意到目标数组有 6 个值,因为它只选择了范围中的第一个“隐藏”行,然后跳过所有其他可见行.

有没有办法获取每个可见行的值而不仅仅是第一行?

您遇到这个问题是因为范围不连续。您不能对非连续范围使用方法Array = Range.Value。您可以通过两种方式来实现您想要的效果。

方法 1 确定范围,遍历单元格并填充数组。适合您处理单列的情况。

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim tbl As ListObject
    Dim ar As Variant
    Dim i As Long, n As Long, ColNumber As Long
    Dim aCell As Range, rng As Range
    
    '~~> Change this to the relevant sheet
    Set ws = Sheet1
    
    '~~> Change this to the relevant table
    Set tbl = ws.ListObjects("Table1")
    
    ws.AutoFilterMode = False
    
    '~~> Change to relevant column number
    ColNumber = 1
    
    '~~> Autofilter as required
    tbl.Range.AutoFilter Field:=ColNumber, Criteria1:="Blah1"
    
    '~~> Set your range
    Set rng = tbl.ListColumns(ColNumber).Range.SpecialCells(12)
    
    '~~> Get the count of cells in that range
    n = rng.Cells.Count
        
    '~~> Resize the array to hold the data
    ReDim ar(1 To n)
        
    n = 1
        
    '~~> Store the values from that range into the array
    For Each aCell In rng.Cells
        ar(n) = aCell.Value
        n = n + 1
    Next aCell
    
    For i = LBound(ar) To UBound(ar)
        Debug.Print ar(i)
    Next i
End Sub

方法 2 确定范围,遍历 Area,然后遍历 Area 中的单元格,然后填充数组。与上面的代码非常相似。

在行动