What's wrong with this code? Countif

Paul Turner

New Member
Joined
Mar 6, 2024
Messages
1
Office Version
  1. 2019
Platform
  1. Windows
I'm trying to calculate how often the digits 1,2,3,4,5 appear in a range. Then I want to do the same for a filtered range. Here is the code:

Set rng = Worksheets("Raw Data").Range("R2:W" & Lastrow)
Worksheets("Analysis").Range("B88").Value = Application.WorksheetFunction.CountIf(rng, 1)
Worksheets("Analysis").Range("C88").Value = Application.WorksheetFunction.CountIf(rng, 2)
Worksheets("Analysis").Range("D88").Value = Application.WorksheetFunction.CountIf(rng, 3)
Worksheets("Analysis").Range("E88").Value = Application.WorksheetFunction.CountIf(rng, 4)
Worksheets("Analysis").Range("F88").Value = Application.WorksheetFunction.CountIf(rng, 5)

ActiveSheet.Range("$A$1:$W$" & Lastrow).AutoFilter Field:=1, Criteria1:="Tue"
Set rng = Worksheets("Raw Data").Range("R2:W" & Lastrow).SpecialCells(xlCellTypeVisible)
Worksheets("Analysis").Range("B86").Value = Application.WorksheetFunction.CountIf(rng, 1) ********This causes an error - "Unable to get the Countif property of the Worksheet function class
Worksheets("Analysis").Range("C86").Value = Application.WorksheetFunction.CountIf(rng, 2)
Worksheets("Analysis").Range("D86").Value = Application.WorksheetFunction.CountIf(rng, 3)
Worksheets("Analysis").Range("E86").Value = Application.WorksheetFunction.CountIf(rng, 4)
Worksheets("Analysis").Range("F86").Value = Application.WorksheetFunction.CountIf(rng, 5)

I can't see why there should be an issue - the bottom block of code is basically the same as the top block except for filtering on the first column. Any ideas?
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
it's possible that there are no visible cells that meet the criteria, which could cause issues with the CountIf function.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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