Aug 16, 2012
How do I count values in filtered cells?
View 6 Replies
After I filter for values in Columns(C), I want to count values in Columns(D). When I filter data, I noticed I can manually select the filtered cell range (i.e. Columns(D)) and at the bottom in the Status Bar it will give the correct count. If I cursor over that status bar count it will display "Number of selected cells that contain data." However, I cannot seem to figure out how to get vba to select just the filtered cells selected in that range and count them only and correctly... it instead counts all the values to the last cell in the filtered range.
1) let's say I have a Range("D1:D120') and there are 20 cells containing data in that range.
2) I filter Columns(C) resulting in 10 rows and the rows are 2, 3, 20, 22, 27, 30, 37, 41, 56, 60. Of those 10 rows, there are only 2 cells containing data (let's say cell D20 and cell D37).
3) Now in reality, cells in Range("D2:D60") actually contain 10 cells with values, but since we filtered on columns(C) values... Columns(D) only shows 2 values.
...... here is my problem:
4) If I manually select the filtered range, the status bar "Count" value will correctly sum as "2." But if I code VBA or even "Record a macro code" (obviously incorreclty on my part), it will return a value of 10 for the range of D2:D60 and not on the visible filtered value which is "2."