I wish to perform calculations on only the visible cells of a filtered table range. I am able to set the filtered range using the .SpecialCells(xlCellTypeVisible) method but when I loop through the resulting range, the code seems to be addressing the non-visible cells.
Data: enter image description here
Filtered Data: enter image description here
Code:
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
Output:
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
So the count and the filtered address range are exactly correct, but the values returned from stepping through filtered range are just the first 4 rows of data, including values that should have been hidden ($A$3 and $A$5). The following is what I expected the results to be:
1 = ABC/address: $A$2
2 = ABC/address: $A$4
3 = XYZ/address: $A$6
4 = XYZ/address: $A$7
Quite the conundrum. I greatly appreciate any help - thank you!
FilteredRecord(h)is equivalent toFilteredRecord.Item(h). Its result is same asFilteredRecord.Cells(h). For a non continuous range, it is NOT a iteration within the target range.Please try.