Error Trying To Get Count of String Value Of A Column In A Filtered Worksheet Using WorksheetFunction.subtotal

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am experimenting with the code below and failing with my results. The line highlighted in red is giving me an "Unable to get the Subtotal property of the Worksheetfunction class" as I try to get a count the number of occurrences of the string "MIN" in column N of my filtered data worksheet. Any thoughts on the correction needed. Are there any other problems with this code?

Rich (BB code):
Sub compDateData(ByRef LDate As Date)
    With ws_ops
        .AutoFilterMode = False
        tarRow = 32
        dtf = LDate
        For i = 1 To 7
            cntDtOps = Application.WorksheetFunction.CountIf(.Columns(2), dtf)
            .Range("A:T").AutoFilter Field:=2, Criteria1:="=" & dtf
            cntstmin = Application.WorksheetFunction.Subtotal(103, .Columns(14), "MIN")
            sumsalt = Application.WorksheetFunction.Subtotal(109, .Columns(14))
            cntsdmin = Application.WorksheetFunction.Subtotal(103, .Columns(15), "MIN")
            sumssand = Application.WorksheetFunction.Subtotal(109, .Columns(15))
            ws_weeks.Cells(tarRow, 14) = Format(dtf, "DDD DD-MMM-YY")
            ws_weeks.Cells(tarRow, 15) = cntDtOps
            ws_weeks.Cells(tarRow, 16) = sumsalt
            ws_weeks.Cells(tarRow, 17) = cntstmin
            ws_weeks.Cells(tarRow, 18) = sumsand
            ws_weeks.Cells(tarRow, 19) = cntsdmin
            tarRow = tarRow + 1
            dtf = dtf + 1
        Next i
    End With
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
I figured out the problem here. 103 is CountA function. I'm needing a Countif function (countif value = "MIN"), which I don't believe exists for the SubToptal function.
Back to Google unless someone can suggest a means of Countif of filtered data. I'll post back if I find a solution.
 
Upvote 0
I tried this suggestion ...
Code:
cntstmin = Application.WorksheetFunction.CountIf(.Cells.SpecialCells(xlCellTypeVisible), "MIN")
but this leaves me with an "Unable to get the CountIf property of the WorksheetFunction class"
 
Upvote 0
You could just use COUNTIFS adding in the same criteria you are filtering on.
 
Upvote 0
Solution
Thank you Rory. If I understand correctly you are suggesting something like this on unfiltered data?

Code:
myvalue = WorksheetFunction.CountIfs(ws_data.columns(1), lupDate, ws_data.columns(14), "MIN")
 
Upvote 0
Yes. It doesn't matter whether the data is filtered or not.
 
Upvote 0

Forum statistics

Threads
1,223,417
Messages
6,171,997
Members
452,438
Latest member
jimmyleung

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top