I need a column to accept one of the following entries only:
be left blank
date (in dd/mm/yyyy) format
or the word 'Validated'
If anything else is input, then an error message is displayed.
I have attached the code. Unfortunately, my problem lies when you type in a valid date, leave the cell and then return to it, the date automatically changes to its numerical value; for example: 15/07/2011 is showing as 40793.
Is there an easier way of formatting the Column?
Regards
Paul S
Tested in Excel 2007
[pre]
[/pre]
be left blank
date (in dd/mm/yyyy) format
or the word 'Validated'
If anything else is input, then an error message is displayed.
I have attached the code. Unfortunately, my problem lies when you type in a valid date, leave the cell and then return to it, the date automatically changes to its numerical value; for example: 15/07/2011 is showing as 40793.
Is there an easier way of formatting the Column?
Regards
Paul S
Tested in Excel 2007
[pre]
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range, CellText As String
If Not Intersect(Target, Columns("A")) Is Nothing Then
For Each Cell In Target
CellText = UCase(Cell.Text)
If Len(CellText) = 0 Then Exit Sub
If CellText = "VALIDATED" Then
Application.EnableEvents = False
Cell.Value = "Validated"
Application.EnableEvents = True
Exit Sub
ElseIf IsDate(CellText) And CellText Like "*[!0-9]*" Then
Application.EnableEvents = False
Cell.Value = CDate(CellText)
Application.EnableEvents = True
Cell.NumberFormat = "dd/mm/yyyy"
Exit Sub
End If
MsgBox "Invalid Entry!" & vbCr & "Please enter one of the following: " & vbCrLf & vbCrLf & "type the text 'validated'" & vbCr & "date (in dd/mm/yyyy format)" & vbCr & "leave blank"
Target.Select
Next
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Cell As Range
If Not Intersect(Target, Columns("A")) Is Nothing Then
For Each Cell In Target
Target.NumberFormat = "General"
Next
End If
End Sub