VBA: check date between 2 dates

LolaM

New Member
Joined
Sep 7, 2011
Messages
27
Hi folks, looking for a bit of help here with the following:

I have a range of cells, holding date values. These cells could also be empty.

I have a 'start' and an 'end' date, and I want to check each date in the range mentioned above falls between the start and end dates.

If date falls between start and end dates, no action. If the date cell is empty, no action.

If date falls outside either the start or end dates, increment a counter.

Repeat for all cells in the range.

I've tried with a For Each and it's successfully moving thru the range, but I'm having issues getting the logic and sequence of the If...Then...Else correct.

I'm back in the world of VBA after a few years of not having to use it, but due to a role change I'm back now and my skills are rusty!

Thanks in advance :)
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Why do you need VBA for this?
You should be able to do that with an Excel formula, like this.

Here is an example, that counts the number of records between cells A2 and A10 that fall between the dates in cells D1 and D2:

1733941287250.png


formula in cell A12:
Excel Formula:
=COUNT(A2:A10)-COUNTIFS(A2:A10,">="&D1,A2:A10,"<="&D2)
 
Upvote 0
Why do you need VBA for this?
You should be able to do that with an Excel formula, like this.

Here is an example, that counts the number of records between cells A2 and A10 that fall between the dates in cells D1 and D2:

View attachment 120278

formula in cell A12:
Excel Formula:
=COUNT(A2:A10)-COUNTIFS(A2:A10,">="&D1,A2:A10,"<="&D2)
Thanks, but it needs to be done in VBA because it's part of an automated checklist of ~40 other questions. The range of dates to be checked will vary in size, the start and end dates are variables in named ranges.
 
Upvote 0
You really haven't given us enough details to create code to do this for you (without us making up the layout - unless you are looking for some sort of generic User Defined Function you can call in VBA).

Where exactly do the Start Date and End Date come from?

What are the rules around the range to check?
Does it always start in a certain cell, and go down to the bottom of that list (which can change)?
Or is it in some range selection?

It might be helpful to show us some sample data and any pertinent VBA code you already have.
 
Upvote 0
OK, I went with creating a generic User Defined Function that you can easily call and re-use, and enter in your variables.

First, here is the code:
VBA Code:
Function CountOutliers(rngData As Range, stDate As Date, endDate As Date) As Long

    Dim cell As Range
    Dim ct As Long
    
'   Loop through all the cells in the data range
    For Each cell In rngData
'       Check to see if non-blank entry in cell and it is a date
        If cell <> "" And IsDate(cell) Then
'           Check to see if the date is before the start date or after the end date
            If cell < stDate Or cell > endDate Then
'               Add one to counter
                ct = ct + 1
            End If
        End If
    Next cell
    
'   Return count of outliers
    CountOutliers = ct

End Function

And here is a test on the example I provided in my earlier reply:
VBA Code:
Sub Test()
    Dim myCount As Long
    myCount = CountOutliers(Range("A2:A10"), Range("D1"), Range("D2"))
    MsgBox myCount
End Sub
When I run it, it returns the number "4", as I would expect from that example.

Note that while I used hard-coded ranges in that Test example above, you can use Range variables instead.

This should do what you want.
 
Upvote 0
Solution
OK, I went with creating a generic User Defined Function that you can easily call and re-use, and enter in your variables.

First, here is the code:
VBA Code:
Function CountOutliers(rngData As Range, stDate As Date, endDate As Date) As Long

    Dim cell As Range
    Dim ct As Long
   
'   Loop through all the cells in the data range
    For Each cell In rngData
'       Check to see if non-blank entry in cell and it is a date
        If cell <> "" And IsDate(cell) Then
'           Check to see if the date is before the start date or after the end date
            If cell < stDate Or cell > endDate Then
'               Add one to counter
                ct = ct + 1
            End If
        End If
    Next cell
   
'   Return count of outliers
    CountOutliers = ct

End Function

And here is a test on the example I provided in my earlier reply:
VBA Code:
Sub Test()
    Dim myCount As Long
    myCount = CountOutliers(Range("A2:A10"), Range("D1"), Range("D2"))
    MsgBox myCount
End Sub
When I run it, it returns the number "4", as I would expect from that example.

Note that while I used hard-coded ranges in that Test example above, you can use Range variables instead.

This should do what you want.
Worked perfectly, thanks so much!
 
Upvote 0

Forum statistics

Threads
1,224,759
Messages
6,180,814
Members
452,996
Latest member
nelsonsix66

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