relaxed_surfer
New Member
- Joined
- Nov 9, 2009
- Messages
- 2
Hi. How can I convert the input of 040210 to the correct date (4th of February 2010) in a date formated field? It gets converted by excel to 1st of Feb. 2010 (!). That's 40210 days after 1st of january 1900. So I don't know how to convert the input to the 4th og february 2010 without also converting the date 1st of February 2010. In Norway, users expect DDMMYY to be converted to the local date format in Excel.
I wrote some VBA code in the to convert different types of input like DDMMYYYY, DDMMYY, DD.MM.YYYY.
Here is an a very simplified code to convert DDMMYY to DD.MM.YYYY (Norwegian date format)
This works fine entering 040210, but it also converts the date 1st of February 2010 to the 4th of February 2010.
So excel does not differ between 040210 and the date 1st of February 2010:
Entering 040210:
Watch : - : Target : 01.02.2010 : Range/Range : Module1.dateFix
: Text : "01.02.2010" : Variant/String : Module1.dateFix
: Value2 : 40210 : Variant/Double : Module1.dateFix
Entering 01.02.2010 (Norwegian date format for 1st of February):
Watch : - : Target : 01.02.2010 : Range/Range : Module1.dateFix
: Text : "01.02.2010" : Variant/String : Module1.dateFix
: Value2 : 40210 : Variant/Double : Module1.dateFix
The problem is that the event Worksheet_Change is fired after Excel has done the conversion internally, so I need a way to capture the user input before this i done in Excel. Any suggestions?
I can't use a text field, since there's a lot of calculations done with the date fields, like calculating number of days for travel compensation, and so on.
I wrote some VBA code in the to convert different types of input like DDMMYYYY, DDMMYY, DD.MM.YYYY.
Here is an a very simplified code to convert DDMMYY to DD.MM.YYYY (Norwegian date format)
Code:
Dim DateFixed As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
If DateFixed = True Then
DateFixed = False
Exit Sub
End If
'Date fix
If Target.NumberFormat = "m/d/yyyy" Then
' If the input is of type DDMMYY with leading zero (Target.Value has length 5)
DateFixed = True
Target.Value = DateValue(Left(Target.Value2, 1) & "." & Mid(Target.Value2, 2, 2) & "." & Right(Target.Value2, 2))
' End If
End If
End Sub
This works fine entering 040210, but it also converts the date 1st of February 2010 to the 4th of February 2010.
So excel does not differ between 040210 and the date 1st of February 2010:
Entering 040210:
Watch : - : Target : 01.02.2010 : Range/Range : Module1.dateFix
: Text : "01.02.2010" : Variant/String : Module1.dateFix
: Value2 : 40210 : Variant/Double : Module1.dateFix
Entering 01.02.2010 (Norwegian date format for 1st of February):
Watch : - : Target : 01.02.2010 : Range/Range : Module1.dateFix
: Text : "01.02.2010" : Variant/String : Module1.dateFix
: Value2 : 40210 : Variant/Double : Module1.dateFix
The problem is that the event Worksheet_Change is fired after Excel has done the conversion internally, so I need a way to capture the user input before this i done in Excel. Any suggestions?
I can't use a text field, since there's a lot of calculations done with the date fields, like calculating number of days for travel compensation, and so on.