Code to Count specific occurances in a sheet?

NessPJ

Active Member
Joined
May 10, 2011
Messages
431
Office Version
  1. 365
Hi all,

I'm looking for a piece of code that will count the number of specified occurances in one of my sheets.

I hope the following explanation will suffice.....

My sheet contains a lot of data like:
Column A: Date
Column B: Time
Column C: Error code
Column D: A formula to show a "1" if a specific error code occurs between 0:00 and 08:00u.
Column E: A formula to show a "1" if a specific error code occurs between 8:01 and 16:00u.

Now, what i want to do is...count the number of 1's in column D and E for a specific Date.
This date is retreived from another cell in the workbook.
 
Ok, no problem.

Pick the cell where you want to define the date, then below

"lRow = Range("A" & Rows.Count).End(xlUp).Row + 1"

add:

nRow = Cells(3, 34)

Where 3,34 is the cell reference. i.e this would be AH,3 (Change accordingly where ever you want to place the date.

Then in every if statement add:

And Cells(i, 1) = nRow

i.e.

If Cells(i, 23).Value = "1" Then
Er6 = Er6 + 1
End If

becomes:

If Cells(i, 23).Value = "1" And Cells(i, 1) = nRow Then
Er6 = Er6 + 1
End If


Make sure you change every statement.

That should work.

You should also change

Dim lRow, nRow As Long

to:

Dim lRow as Long
Dim nRow as Date
 
Last edited:
Upvote 0

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hey there,

Thanks a million for all the help! :)
I fiddled around a little with the code and its working now!

One more question however...
Would it be possible to refer to a Date in a Cell that is not on the same Worksheet?

Will i have to use:
nRow = Sheets.("Sheet1").Cells(3, 34) ?
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,850
Members
452,948
Latest member
UsmanAli786

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