我希望只对过滤后的表格区域的可见单元格执行计算。我可以使用.SpecialCells(xlCellTypeVisible)方法设置过滤后的区域,但当我循环遍历结果区域时,代码似乎在处理不可见的单元格。
数据:enter image description here
过滤数据:enter image description here
代码:
Dim Filter_Range As String
Dim h As Integer
Set FilteredRecord = shTable.Range("A2:A7").SpecialCells(xlCellTypeVisible)
Debug.Print "Filtered count: " & FilteredRecord.Count
Debug.Print "Filtered address: " & FilteredRecord.Address
For h = 1 To FilteredRecord.Count
Debug.Print h & " = " & FilteredRecord(h) & "/address: " & FilteredRecord(h).Address
Next
字符串
输出量:
A2:A7
Filtered count: 4
Filtered address: $A$2,$A$4,$A$6:$A$7
1 = ABC/address: $A$2
2 = DEF/address: $A$3
3 = ABC/address: $A$4
4 = DEF/address: $A$5
型
因此,计数和筛选的地址范围是完全正确的,但是从筛选范围中返回的值只是前4行数据,包括应该隐藏的值($A$3和$A$5)。以下是我期望的结果:
真是个难题,我非常感谢任何帮助-谢谢!
3条答案
按热度按时间zsohkypk1#
FilteredRecord(h)
等效于FilteredRecord.Item(h)
。其结果与FilteredRecord.Cells(h)
相同。对于非连续范围,它不是目标范围内的迭代。请试试看。
字符串
hmtdttj42#
好吧,这很有趣-我在提出我的问题后就解决了它。我不应该用索引遍历范围,我应该在过滤范围上使用For Each。以下修改后的代码工作得很好:
字符串
输出为:
型
ht4b089n3#
这个结果的解释是,如果一个区域包含不连续的单元格,则结果Range对象具有“区域”,即连续区域的列表。
这可以通过范围的
Areas
属性来实现。试试这个
字符串