Vba: Application.WorksheetFunction.CountIfs with variables

Nelson78

Well-known Member
Joined
Sep 11, 2017
Messages
526
Office Version
  1. 2007
Hello everybody.

I'm tinkering with the following situation:

VBA Code:
cell.value = Application.WorksheetFunction.CountIfs(Sheets(1).Range("B2:B" & lr), ">=" & date1, Sheets(1).Range("B2:B" & lr), "<=" & date2)

The range Sheets(1).Range("B2:B" & lr) is populated only by dates.
In cell I need to count all the dates included in the range delimited by the variablese date1 and date2.

At the moment the result is 0, but it is not correct.

How can I figure it out?

Thank's in advance.
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Try converting the dates using CLng

Rich (BB code):
cell.Value = Application.WorksheetFunction.CountIfs(Sheets(1).Range("B2:B" & lr), ">=" & CLng(date1), Sheets(1).Range("B2:B" & lr), "<=" & CLng(date2))

If that doesn't work show us how you have Dim'd date1 and date2 and how you are populating them and check that column B is being recognised as a date (/number) and not as text.
 
Upvote 0
Solution

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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