Hi Everyone,
Like many others before me I have the often asked question of how to create an 'Access-like' input mask for hastening date data entry.
Using C Pearson's much quoted code I've modified it as such:
Note: I'm aware that most of the time I'm simply re-typing the original code - it was a learning experiencing and I also realised that the different American and Australian date formatting was less of a programming issue due to the systems being set up differently in the first place.
My issue is having run tests using only 4 digit entry, all types of digit entry, with and without DateValue on the final DateStr, changing the cell format to text, to general, to date - always results in a code like "8811" (i.e. 08/08/2011) being changed into 01/01/1924 (approximately) .
At first I assumed the use of "DateValue" in assigning the final .Formula value was forcing this conversion. However, with or without DateValue it still seems to read the figure as a DateValue and convert it as such.
I'm sure it has to be something elemental that I'm missing given the scores of people I alone have seen on boards reading C Pearson's code and not coming up with another peep.
Any help most appreciated!
Cheers
James
Like many others before me I have the often asked question of how to create an 'Access-like' input mask for hastening date data entry.
Using C Pearson's much quoted code I've modified it as such:
Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim DateStr As String
On Error GoTo EndMacro
If Application.Intersect(Target, Range("C2:C1000")) Is Nothing Then
Exit Sub
End If
If Target.Cells.Count > 1 Then
Exit Sub
End If
If Target.Value = "" Then
Exit Sub
End If
Application.EnableEvents = False
With Target
If .HasFormula = False Then
Select Case Len(.Formula)
Case 4
'.Formula = Format(.Text, "0000")
' e.g. 1811 = 01/08/2011
DateStr = Left(.Value, 1) & "/" & Mid(.Value, 2, 1) & "/" & Right(.Value, 2)
' e.g., 9298 = 2-Sep-1998
'DateStr = Left(.Formula, 1) & "/" & _
' Mid(.Formula, 2, 1) & "/" & Right(.Formula, 2)
Case 5
' e.g. 11011 = 01/10/2011
DateStr = Left(.Formula, 1) & "/" & Mid(.Formula, 2, 2) & "/" & Right(.Formula, 2)
' e.g., 11298 = 12-Jan-1998 NOT 2-Nov-1998
'DateStr = Left(.Formula, 1) & "/" & _
' Mid(.Formula, 2, 2) & "/" & Right(.Formula, 2)
Case 6
' e.g. 121011 = 12/10/2011
DateStr = Left(.Formula, 2) & "/" & Mid(.Formula, 3, 2) & "/" & Right(.Formula, 2)
' e.g., 090298 = 2-Sep-1998
'DateStr = Left(.Formula, 2) & "/" & _
' Mid(.Formula, 3, 2) & "/" & Right(.Formula, 2)
Case 7
' e.g. 5102011 = 05/10/2011
DateStr = Left(.Formula, 1) & "/" & Mid(.Formula, 2, 2) & "/" & Right(.Formula, 4)
' e.g., 1231998 = 23-Jan-1998 NOT 3-Dec-1998
'DateStr = Left(.Formula, 1) & "/" & _
' Mid(.Formula, 2, 2) & "/" & Right(.Formula, 4)
Case 8
' e.g. 17082011 = 17/08/2011
DateStr = Left(.Formula, 2) & "/" & Mid(.Formula, 3, 2) & "/" & Right(.Formula, 4)
' e.g., 09021998 = 2-Sep-1998
'DateStr = Left(.Formula, 2) & "/" & _
' Mid(.Formula, 3, 2) & "/" & Right(.Formula, 4)
Case Else
Err.Raise 0
End Select
[B]'.Formula = DateValue(DateStr) '- Don't want a date VALUE
.Formula = DateValue(DateStr)[/B]
End If
End With
Application.EnableEvents = True
Exit Sub
EndMacro:
MsgBox "You did not enter a valid date."
Application.EnableEvents = True
End Sub
My issue is having run tests using only 4 digit entry, all types of digit entry, with and without DateValue on the final DateStr, changing the cell format to text, to general, to date - always results in a code like "8811" (i.e. 08/08/2011) being changed into 01/01/1924 (approximately) .
At first I assumed the use of "DateValue" in assigning the final .Formula value was forcing this conversion. However, with or without DateValue it still seems to read the figure as a DateValue and convert it as such.
I'm sure it has to be something elemental that I'm missing given the scores of people I alone have seen on boards reading C Pearson's code and not coming up with another peep.
Any help most appreciated!
Cheers
James
Comment