VBA Check if date is valid in a certain format DD.MM.YYYY

antaeusguy

Board Regular
Joined
Mar 8, 2010
Messages
81
Hi

My Regional settings for date in my laptop is MM/DD/YYYY (American format)

I've a sheet, which the cell A1 has custom format DD.MM.YYYY

Now, I want to check if the date in A1 is valid or not by using a VBA code

Say, in A1 it is 31.11.2012 (Not valid as November has no 31)

How should I program the VBA code so that the check will return FALSE?

I just need a Boolean value

Any help is very much appreciated! :)
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Here's one way:
Code:
Sub DateCheck()

    Dim myDate As String
    Dim myDateCheck As Boolean
    
    myDate = Format((DateSerial(Right(Range("A1"), 4), Mid(Range("A1"), 4, 2), Left(Range("A1"), 2))), "dd.mm.yyyy")
    myDateCheck = (Range("A1") = myDate)
    MsgBox myDateCheck
    
End Sub
 
Upvote 0
You could alternatively apply Data Validation to the cell and specify Date as allowed. This would only allow dates to be entered in the cell (and you can ensure correct formatting is applied via Format>Cells>Custom).
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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