VBA CountIf Function

mikeymay

Well-known Member
Joined
Jan 17, 2006
Messages
1,648
Office Version
  1. 365
Platform
  1. Windows
I have a worksheet that contains some product prices with a Date From value.

Currently all the dates in the table are set to 1 Jan 25 except the final row of the table (row 3478) set as 2 Jan 25.
The column containing the dates is formatted as a date format
1736259128842.png



Using the TODAY() funtion that returns todays date of 7 Jan 25, when I do an Excel function to count the number of instances of a date value > todays date (7 Jan 25) - 6 days to establish how many instances of dates >1 Jan 25, I get a return of 1 as based on the above image
1736259287941.png


And when i want to see how many instances there are of date values > TODAY()-7 (31 Dec 24) I get a return of 3,472 which is the exact count or rows in the table
1736259572348.png


When I want to do the same in VBA
The current date - 6 days (i.e. 1 Jan 25) returns 1 as expected
1736259753636.png


But doing the same for the current date - 7 (i.e. 31 Dec 24) I get 0 when it should return 3,472
1736259810440.png


This makes no sense at all and I have no idea what Excel/VBA is doing.....

Can anyone shed some light on this?


TIA
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Which VBA module are you running that from?
A sheet module? A general module?
Are you sure that the sheet with your data is the active (selected) sheet when you run the code?

If I create a General module, drop this code in it, and make sure I am on the sheet with my data and run the code, it returns the expected results for me:
VBA Code:
Sub MyTest()
    MsgBox WorksheetFunction.CountIf(Range("E7:E3478"), ">" & Date - 7)
    MsgBox WorksheetFunction.CountIf(Range("E7:E3478"), ">" & Date - 6)
End Sub
This returns two messages boxes, with the counts of each scenario, and it works as expected for me.
 
Upvote 0
I am using the immediate windows to test but the the code that I am running where I experienced the issue is in a user form.

The dtDateFrom variable is declared publicly in a General module.
 
Upvote 0
If @Joe4's code does not give you the correct result try adding CLng. eg
Rich (BB code):
Sub MyTest()
    MsgBox WorksheetFunction.CountIf(Range("E7:E3478"), ">" & CLng(Date - 7))
    MsgBox WorksheetFunction.CountIf(Range("E7:E3478"), ">" & CLng(Date - 6))
End Sub
 
Upvote 0
Rather than trying to establish why this isn't working as it should, I'm going to drop the variables used in the COUNITFS into cells and watch the cell containing the formula.

This works as expected so I'll use this is the solution.

I am curious as to why the VBA version doesn't work though...
 
Upvote 0
Did you try my version ?
You are in the UK which means that your date format is most likely dd/mm/yyyy, since VBA works in US Date format it is likely that the issue is related to the date format being supplied to the CountIf formula using VBA doesn't match.
Using CLng should fix that.
 
Upvote 0

Forum statistics

Threads
1,225,358
Messages
6,184,493
Members
453,236
Latest member
Siams

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