netrixuser
Board Regular
- Joined
- Jan 21, 2019
- Messages
- 77
- Office Version
- 365
- Platform
- Windows
Hello all, I was looking for a way to filter a column and then count the number of entries in that column and assign that number to a variable. I'm assuming the code I have written is incorrect but the weird thing is that I get the correct result when I filter on one column and use my count "formula" but when I remove that filter an use the same formula (different variable assigned) for another filtered column, I get the wrong number returned for the number of visible cells.
In the VBA code pasted below - it is the count for column P that returns the wrong value whereas the preceding count for column N is correct:
This returns 72 which i the number of cells in column N when filtered
GTVS = Range("N1:N" & lastRow).SpecialCells(xlCellTypeVisible).Count - 1
Whereas this returns 404 which is the value of the variable lastRow minus 1 and not the number of cells in column P when filtered
MOBL = Range("P1:P" & lastRow).SpecialCells(xlCellTypeVisible).Count - 1
I tried just selecting column P (ie removing & lastRow) but that counts all cells in the column
Thanks in advance for any/all guidance !
Regards
Netrix
In the VBA code pasted below - it is the count for column P that returns the wrong value whereas the preceding count for column N is correct:
This returns 72 which i the number of cells in column N when filtered
GTVS = Range("N1:N" & lastRow).SpecialCells(xlCellTypeVisible).Count - 1
Whereas this returns 404 which is the value of the variable lastRow minus 1 and not the number of cells in column P when filtered
MOBL = Range("P1:P" & lastRow).SpecialCells(xlCellTypeVisible).Count - 1
I tried just selecting column P (ie removing & lastRow) but that counts all cells in the column
Thanks in advance for any/all guidance !
Regards
Netrix
VBA Code:
Option Explicit
Dim lastRow As Integer
'Dim K As Integer
Dim GTVS As Integer
Dim MOBL As Integer
Dim SMS As Integer
Dim Whats As Integer
Dim WeCh As Integer
Sub filterCount_GTVS()
Dim sh As Variant
'Check if sheet exists - delete if yes
For Each sh In ActiveWorkbook.Worksheets
If sh.Name = "Statistics" Then
Application.DisplayAlerts = False
sh.Delete
Application.DisplayAlerts = True
End If
Next sh
Worksheets("MRL_Report").ShowAllData
'find last row of the worksheet - for this example lastRow = 405
lastRow = Sheets("MRL_Report").Range("N" & Rows.Count).End(xlUp).Row
'Filter column A for certain users
Sheets("MRL_Report").Cells.CurrentRegion.AutoFilter Field:=1, Criteria1:=Array( _
"BPIM", "GPTI", "RPTE"), Operator:=xlFilterValues
'Filter column N for colour pink
Sheets("MRL_Report").Range("$N$1" & lastRow).AutoFilter Field:=14, Criteria1:=RGB(255, _
199, 206), Operator:=xlFilterCellColor
'Count visible cells in column N - start at header row and then delete "1" from total to get around filter returning no cells
GTVS = Range("N1:N" & lastRow).SpecialCells(xlCellTypeVisible).Count - 1
'THE LINE ABOVE WORKS CORRECTLY AND RETURNS 72 FOR THE VARIABLE GTVS
'Add new sheet - add values from count above
Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Statistics"
Sheets("Statistics").Range("B2").Value = "GTVS"
Sheets("Statistics").Range("B3").Value = GTVS
'Remove filter from GTVS column N (pink) but keep filter on column A
Sheets("MRL_Report").Cells.CurrentRegion.AutoFilter Field:=14
'Filter column P for colour pink
Sheets("MRL_Report").Cells.CurrentRegion.AutoFilter Field:=16, Criteria1:=RGB(255, _
199, 206), Operator:=xlFilterCellColor
'Count visible cells in column P
MOBL = Range("P1:P" & lastRow).SpecialCells(xlCellTypeVisible).Count - 1
' ****** THE LINE ABOVE, IDENTICAL TO THE FIRST COUNT EXCEPT THE VARIABLE NAME AND COLUMN, RETURNS 404 - THIS IS THE VALUE FOR lastRow MINUS 1 ***********
'Add values from counts
Sheets("Statistics").Range("C2").Value = "MOBL"
Sheets("Statistics").Range("C3").Value = MOBL
'Remove filter from MOBL column N
Sheets("MRL_Report").Range("$A$1:$A$405").AutoFilter Field:=16