VBA Excel: Date Format
Solved
misfit
-
Kimo -
Kimo -
Hello,
I can't format a date entered in a form:
When closing the form, the entered data is copied into cells of a spreadsheet; I have set the cells in the first column of my spreadsheet to a "Date" format in the form "dd/mm/yyyy".
In the module (see code below), I force the value of the textbox to this format:
However, despite all these precautions, the date copied into the cell is in the format "mm/dd/yyyy".
I don't want to change the regional preferences.
Does anyone have a solution?
Thank you in advance.
I can't format a date entered in a form:
When closing the form, the entered data is copied into cells of a spreadsheet; I have set the cells in the first column of my spreadsheet to a "Date" format in the form "dd/mm/yyyy".
In the module (see code below), I force the value of the textbox to this format:
If saisie.date_ev.Value <> "" Then If Not IsDate(saisie.date_ev.Value) Then MsgBox "Incorrect date.", vbCritical + vbOKOnly, "Error" saisie.date_ev.Value = "" saisie.date_ev.SetFocus saisie.Hide saisie.Show Else saisie.date_ev.Value = Format(saisie.date_ev.Value, "dd/mm/yyyy") End If End If
However, despite all these precautions, the date copied into the cell is in the format "mm/dd/yyyy".
I don't want to change the regional preferences.
Does anyone have a solution?
Thank you in advance.
21 réponses
- 1
- 2
Suivant
Hello Misfit,
The date formats assigned to cells in Excel do not all translate the same way in VBA.
Thus, a cell formatted as dd/mm/yy in Excel is translated in VBA as: NumberFormat = dd/mm/yy. Normal.
Whereas a format defined as dd/mm/yyyy is translated as: NumberFormat = m/d/yyyy, which changes everything.
In practice, in VBA, reverse the months and the days.
Armojax.
The date formats assigned to cells in Excel do not all translate the same way in VBA.
Thus, a cell formatted as dd/mm/yy in Excel is translated in VBA as: NumberFormat = dd/mm/yy. Normal.
Whereas a format defined as dd/mm/yyyy is translated as: NumberFormat = m/d/yyyy, which changes everything.
In practice, in VBA, reverse the months and the days.
Armojax.
- 1
- 2
Suivant
Thanks for your reply, which works really well as well.
These Americans are crazy...
ActiveCell.NumberFormat = "mm/dd/yyyy" 'date/month reversed
Americans are not crazy at all... Excel is simply of American origin and the Excel interface is adapted according to the different countries except for VBA which has remained American so everything from there like the date format has to be adapted to the different interfaces of Excel.
I don't know much, but from what I see and how VBA is written, the deduction is obvious...
;)