VBA Excel: Date Format

Solved
misfit -  
 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:
 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
Armojax
 
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.
32
misfit
 
Hi Armojax,

Thanks for your reply, which works really well as well.
These Americans are crazy...
0
pat
 

ActiveCell.NumberFormat = "mm/dd/yyyy" 'date/month reversed
0
pat
 
Actually, the problem persists in my case (the recording of a date-type cell in a CSV file is done according to the format "dd/mm/yyyy" but reloading the cell from the file changes the display format to "mm/dd/yyyy". Apparently, save and load don't work with the same format! I was able to bypass this by adding a quote (') in front of the value to be assigned: ActiveCell.Value = "'" + val_cell_read_from_file
0
jimmy1120112 Posted messages 713 Status Membre 61
 
@ misfit
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...
;)
0
Kimo
 
Thank you very much, dear friend. This small but no less brilliant explanation frees me from countless sleepless nights spent twisting my brain over this tiny hiccup... Sir, your clarification on the reversal of days and months in VBA is simply, if I may twist the tongue of Voltaire, "Eureka-like." Best regards...
0
  • 1
  • 2