To check if the dates in an Excel column follow the dd/mm/yyyy format and flag them as 1 if they do not, you can use a formula. Below are suggestions for how to accomplish this.
Method 1: Using a Formula
Suppose your dates are in the range A1:A10. You can place the following formula in cell B1 and drag it down to check each date:
=IF(AND(ISNUMBER(A1), TEXT(A1, "dd/mm/yyyy") <> TEXT(A1, "dd/mm/yyyy")), 1, 0)
Explanation of the Formula
ISNUMBER(A1): Checks if the value in cell A1 is a number (dates are stored as serial numbers in Excel).
TEXT(A1, "dd/mm/yyyy"): Converts the date in A1 to the dd/mm/yyyy format.
<>: Compares the converted text to itself to see if it appears to be in the correct form. If it's a valid date, it should match.
AND(...): Combines the two checks to ensure both conditions are met.
IF(...): If the conditions are not met, it returns 1, otherwise it returns 0.
Adjustments for Non-Date Values
The above formula may not work well to strictly enforce the format (e.g., for dates that are input as text). You can adjust the logic to check if the input is genuinely a date and to catch non-standard input formats which may not be valid dates at all.
Improved Method to Check Format
If you want to ensure that the values strictly adhere to the dd/mm/yyyy format in a string sense, you could theoretically use a more sophisticated direct string comparison if the input type is text:
=IF(AND(LEN(A1)=10, MID(A1, 3, 1)="/", MID(A1, 6, 1)="/", AND(ISNUMBER(VALUE(LEFT(A1, 2))), ISNUMBER(VALUE(MID(A1, 4, 2))), ISNUMBER(VALUE(RIGHT(A1, 4))))), 0, 1)
Explanation:
LEN(A1)=10: Checks if the length of the input is exactly 10 characters.
MID(A1, 3, 1)="/": Checks if the third character is a slash (/).
MID(A1, 6, 1)="/": Checks if the sixth character is a slash (/).
ISNUMBER(...) and VALUE(...): Ensures that the substrings that represent the day, month, and year are numeric.
IF(...): Returns 0 if the date format is valid; otherwise, it returns 1.
Method 2: Using a VBA Function
If you prefer a VBA function to check for correct formatting, here is a simple function you can use:
- Open the VBA Editor by pressing
ALT + F11.
- Insert a new module by right-clicking on any entry for your workbook in the Project Explorer and selecting
Insert > Module.
- Copy and paste the following code:
Function CheckDateFormat(dateValue As Variant) As Integer
Dim parts As Variant
If IsEmpty(dateValue) Then
CheckDateFormat = 1
Exit Function
End If
If Not IsDate(dateValue) Then
CheckDateFormat = 1
Exit Function
End If
parts = Split(Format(dateValue, "dd/mm/yyyy"), "/")
If UBound(parts) = 2 Then
If Not IsNumeric(parts(0)) Or Not IsNumeric(parts(1)) Or Not IsNumeric(parts(2)) Then
CheckDateFormat = 1
Exit Function
End If
If Len(parts(0)) <> 2 Or Len(parts(1)) <> 2 Or Len(parts(2)) <> 4 Then
CheckDateFormat = 1
Else
CheckDateFormat = 0
End If
Else
CheckDateFormat = 1
End If
End Function
- Close the VBA editor and return to Excel.
Now, you can use this function in Excel like this:
=CheckDateFormat(A1)
This will return 1 if the date in cell A1 does not follow the dd/mm/yyyy format and 0 otherwise.
Method 3: Using Conditional Formatting
To visually flag dates that do not meet the dd/mm/yyyy format, you can apply conditional formatting:
-
Select the range of dates (for example, A1:A10).
-
Go to the Home tab on the Ribbon.
-
Click on Conditional Formatting > New Rule.
-
Select Use a formula to determine which cells to format.
-
Enter the following formula:
=OR(ISTEXT(A1), ISERROR(DATEVALUE(A1)), MID(A1, 3, 1)<>"/", MID(A1, 6, 1)<>"/")
-
Choose the formatting style (e.g., fill color) to apply to cells that do not have the correct date format.
-
Click OK to apply the rule.
Summary
You can use Excel formulas, custom VBA functions, or conditional formatting to check and flag dates that do not follow the dd/mm/yyyy format. Choose the method that best fits your needs and your comfort level with Excel features!