Evaluating Dates in VBA

diamantebonita

New Member
Joined
May 25, 2011
Messages
22
Arrrrgh!! how come dates are so hard to work with? I really thought this was gonna be a quick and easy program to write.

I want to check the date of the items in the list and determine if the date is today and/or within 7 days from today. This is how I determine the list that will be displayed. But i'm not able to get any of the DATE functions or statements to work.

Private Sub Workbook_Open()
Dim msg As String
Dim vendorL As String
Dim amountL As String
Dim duedateL As String



Sheets("Bills").Activate
Range("G2").Activate

If ActiveCell.Value = WorksheetFunction.Date() And ActiveCell.Value < WorksheetFunction.Date() + 7 Then
vendorL = ActiveCell.Offset(0, 2)
amountL = ActiveCell.Offset(0, 1)
duedateL = ActiveCell.Value
End If

Dim TodayDate As String



TodayDate = Format(Date, "Long Date")

msg = "Hello Genese!" & vbCrLf & vbCrLf
msg = msg & "Today is " & TodayDate & "." & vbCrLf
msg = msg & "These are the bills that need to be paid this week." & vbCrLf & vbCrLf
msg = msg & vendorL & vbTab & amountL & vbTab & duedateL & vbCrLf
MsgBox msg, vbInformation + vbOKOnly, "Bills To Pay"



End Sub
 
Last edited by a moderator:

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
The logic is almost there...

If ActiveCell.Value = WorksheetFunction.Date() And ActiveCell.Value < WorksheetFunction.Date() + 7 Then

Read that, and think about it.....

If activecell.value is say Today+3, then it certainly does not = Today, therefor the AND is false.


Also, nothing wrong with the way you did it..but you don't need to use the worksheetfunction.Date...
VBA has it's own built in Date function...called..... Date.


Try

If ActiveCell.Value >= Date And ActiveCell.Value < Date + 7 Then


Hope that helps...
 
Upvote 0
If ActiveCell.Value = WorksheetFunction.Date() And ActiveCell.Value < WorksheetFunction.Date() + 7 Then

Use Date instead of WorksheetFunction.Date(). I think you want,
Code:
If ActiveCell.Value <= Date +7 Then
 
Upvote 0

Forum statistics

Threads
1,224,606
Messages
6,179,866
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