VBA script for comparing dates

viper7986

New Member
Joined
Apr 1, 2013
Messages
9
I have a macro that has a date compare function, my problem is that i want it to ignore the year of the date so that the sheet can be re-used each year without changing the DateValue to the following year.

here is my code:

Sub lock_aug()
If Date < DateValue("8/31/2013") Then
Run "Lock_Date_Error.Lock_Date_Error"
Exit Sub
End If
ActiveSheet.Unprotect

If Date > DateValue("8/31/2013") Then
Range("ds5:ei70").Value = Range("ds5:ei70").Value
End If

If Date > DateValue("8/31/2013") Then
Range("ds5:ei70").Locked = True
End If

ActiveSheet.Protect Contents:=True
End Sub


i would like to do something like:
If date > datevalue (8/31/Year())

Is this even possible?
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

dermie_72

Well-known Member
Joined
Sep 4, 2012
Messages
1,540
try this:
A = Year(today())
If Date < DateValue("8/31/" & A) Then

Then you can just use the A variable for you year across the whole macro.
 
Upvote 0

viper7986

New Member
Joined
Apr 1, 2013
Messages
9
try this:
A = Year(today())
If Date < DateValue("8/31/" & A) Then

Then you can just use the A variable for you year across the whole macro.

I tried the above and i get a compile error: sub or function not defined

Sub lock_apr()
A = Year(today())
If Date < DateValue("4/30" & A) Then
Run "Lock_Date_Error.Lock_Date_Error"
Exit Sub
End If

i beleive i figured it out, i used

Sub lock_apr()
If Date < DateValue("4/30") Then
Run "Lock_Date_Error.Lock_Date_Error"
Exit Sub
End If

The script ran without any issues and performed what i wanted
 
Last edited:
Upvote 0

dermie_72

Well-known Member
Joined
Sep 4, 2012
Messages
1,540
Sorry, My bad on the script, if you had used:

A = Year(Now)

instead it would have worked too :)
 
Upvote 0

Forum statistics

Threads
1,196,007
Messages
6,012,831
Members
441,732
Latest member
Ayon

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
Top