Vba get exact number of filtered data in Excel
For example, I want to filter records pertaining to only Barbara and do some processing. For doing processing of the filtered records, I need to know the number of filtered records.
To do it in Excel, here is the answer:
- Option Explicit
- Sub CountFilteredData()
- Dim sName As String
- Dim rngArea As Range
- Dim lngRecordCount As Long
- sName = "Barbara"
- 'Filter rows based on Name which is Field 2 (Col AQ).
- ActiveSheet.Range("AP4:AR14").AutoFilter Field:=2, Criteria1:=sName
- lngRecordCount = 0
- For Each rngArea In ActiveSheet.Range("AP4:AR14").SpecialCells(xlCellTypeVisible).Areas
- lngRecordCount = lngRecordCount + 1
- 'Remove filter that was applied.
- ActiveSheet.AutoFilterMode = False
- End Sub
a) Line 13 - RecordCount is the variable corresponding to number of filtered records.
b) Line 14, 15,16 - Loop through the filtered data and count number of rows.