Compare two dates against the system date

gareth1979

New Member
Joined
May 28, 2018
Messages
5
In simple term I want to create a simple vb script in excel that looks at the system date and then adds 1 to a cell on my sheet if it falls between a particular date range for example my dates 01/05/2018 to 31/05/2018 the system date is 28/05/2018 so adds 1 to my spreadsheet
I have tried playing around with numerous way but I am not getting anywhere
Public Sub test()
Dim d As Date
d = CDate(29 / 5 / 2019)
n = Now()
Range("a1").Value = d
If Year(d) = Year(n) And Month(d) = Month(n) Then
MsgBox "It works"
Else
MsgBox "It doesnt work"
End If
End Sub
Public Sub tryThis()

Dim xdate As Date
xdate = "4 / 27 / 2018"
Range("a1") = xdate
If Month(d) = Month(xdate) And Year(Date) = Year(xdate) Then
MsgBox "OK"
Else
MsgBox "not OK"
End If
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Welcome to Mr Excel forum

The current year is 2018, not 2019, and you should use DateSerial
Try
Code:
Public Sub test()
Dim d As Date, n As Date

[COLOR=#ff0000]d = DateSerial(2018, 5, 29)[/COLOR]
n = Now()

Range("a1").Value = d
If Year(d) = Year(n) And Month(d) = Month(n) Then
    MsgBox "It works"
Else
    MsgBox "It doesnt work"
End If
End Sub

M.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,022
Latest member
RobertV1609

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