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

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
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,225,761
Messages
6,186,891
Members
453,383
Latest member
SSXP

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