How describe periode of time in excisting vba code

Wil Moosa

Well-known Member
Joined
Aug 11, 2002
Messages
893
Good to be back...

I have the following code that works well for my needs. How can I adjust it in that way it searches for all past dates ánd all the dates from now up to fifeen days? Thank you.

Sub DueDateCheck()
Dim FindString As Date
Dim Rng As Range
FindString = CLng(Date)
With Sheets("Teamsamenstelling").Range("AA6:A131")
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Worksheets("Teamsamenstelling").Activate
MsgBox "There are one of more dates due"
Else
Worksheets("Monitor").Activate
MsgBox "Nothing found"

End If
End With
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Solved it in a complete different way.

With =COUNTIF(AA6:AA131,"<"&D160) - where D160 is the cell with the formuatie = TODAY()+15 - I counted the number of date entries for =TODAY+15. Then I used this code:

Private Sub Workbook_Open()
'Due date check
Application.ScreenUpdating = False
Sheets("Teamsamenstelling").Select
If Range("D161").Value > "0" Then
MsgBox " One or more date entries have expired or will expire within 15 days", _
Buttons:=vbExclamation
Else
Sheets("Monitor").Select
End If
Application.ScreenUpdating = True

End Sub

It works for me.
 
Last edited:
Upvote 0
Solution
The Now() and Date() functions can be manipulated like so:

(try these in your immediate window in the VBE)
Debug.Print Now - 15
Debug.Print Date - 15
Debug.Print Now + 15
Debug.Print Date + 15

With that you can use these in code like:
If Range("A1") < Date Then MsgBox "Past Due"
If Range("A1") > Date And Range("A1") < Date + 14 Then MsgBox "Due within the next two weeks"
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,348
Members
452,638
Latest member
Oluwabukunmi

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