我目前正在尝试将过滤后的列复制到数组中以填充 Powerpoint 演示文稿中的 ComboBox。
我用来执行此操作的代码行是:
ar = tbl.ListColumns(ColNumber).Range.SpecialCells(12).Value
其中“ar”是目标数组,“tbl”是源表,“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
中的单元格然后填充数组。与上面的代码非常相似。在行动
关于excel - SpecialCells(12).Value 在第一个隐藏行后停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62656055/