Comparing dates in an if then statement

Bagsy

Active Member
Joined
Feb 26, 2005
Messages
467
Office Version
  1. 365
Platform
  1. Windows
I wonder if anybody can help a bit here, please
I have on a sheet called Calibrations due
cell M3 has the formula =Today()
Cell M4 = last run date of my code, this is part of my code as below, copy current date & paste values only in Cell M4
Cell N4 has the formula =EOMONTH(M4,0)- M4 (this is so I can add the days to get the end of the month)
Cell M5 has the formula =M4+N4 (this gives me the last day of the month)
Cell M6 has the formula =M5+1 (this give me the earliest next run time for the code.

The reason I am doing this is as this is an OnOpen event and I only want the code to run once a month but as near to the first of the month as possible. Obviously as this runs only when opened it could run at anytime during the month from the 1st onwards.

I am having trouble with the line of code below, where it checks if the current date is less than or equal to the last day of the month, if it is I just need to exit the sub. The problem is it exits the sub whether the current date is before or after the last day of the month.
Any help is much appreciated
VBA Code:
If Sheets("Calibration Due").Range("M3") <= ("M5") Then 'This is comparing current date to the end of the month date , if current date is less or equal to end of month exit sub

Any help is always appreciated
VBA Code:
Private Sub Workbook_Open()
'this is running the code to copy & email all equipment that requires calibration
Sheets("Calibration").Select
If ThisWorkbook.ReadOnly = True Then
Exit Sub
End If

Application.ScreenUpdating = False
'Sheets("Calibration Due").Activate
'Sheets("Calibration Due").Visible = True
If Sheets("Calibration Due").Range("M3") <= ("M5") Then 'This is comparing current date to the end of the month date , if current date is less or equal to end of month exit sub
Exit Sub

Else
Sheets("Calibration Due").Visible = True
Sheets("Calibration Due").Activate

Call ListCalibrationDueItems
Call EmailCalibrationDue

Range("M3").Copy 'copying the current date
Range("M4").PasteSpecial xlPasteValues 'pasting current date into last run time
Sheets("Calibration").Select
Sheets("Calibration Due").Visible = False
ActiveWorkbook.Save
Application.ScreenUpdating = True

End If
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
JoeMo was quicker than me, but let me suggest to qualify also the second term:
VBA Code:
If Sheets("Calibration Due").Range("M3") <= Sheets("Calibration Due").Range("M5") Then

Bye
 
Upvote 0
JoeMo was quicker than me, but let me suggest to qualify also the second term:
VBA Code:
If Sheets("Calibration Due").Range("M3") <= Sheets("Calibration Due").Range("M5") Then

Bye
Thank you so much Anthony, really appreciated
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,849
Members
449,051
Latest member
excelquestion515

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