Non, il ne faut pas coller des baffes aux clients qui ... paient.
A la place, si l'on suppose que les dates seront mises dans la colonne B, à partir de B2, voici comment les formatter :
LineCount = rst.RecordCount
For iLine = 1 To LineCount
xlWs.Cells(iLine + 1, 2).NumberFormat = "dd/mm/yyyy"
Next |
En prime, il est possible d'ajuster la largeur et la hauteur des colonnes :
xlApp.Selection.CurrentRegion.Columns.AutoFit
xlApp.Selection.CurrentRegion.Rows.AutoFit
|
Et pourquoi ne pas aussi insérer le titre des colonnes ? Pendant que j'y suis, voici mon exemple en entier :
'Extract data to excel file
Private Sub B_export2excel_Click()
Dim SQL_ligne As String, s As String
'Dim bds As Database, rst As Recordset
' Retrieve data from table
SQL_ligne = "SELECT id, date1, text1" & _
" FROM T_test;"
Set bds = CurrentDb
Set rst = bds.OpenRecordset(SQL_ligne)
' Create an instance of Excel and add a workbook
Set xlApp = CreateObject("Excel.Application" )
Set xlWb = xlApp.Workbooks.Add
Set xlWs = xlWb.Worksheets(1)
' Display Excel and give user control of Excel's lifetime
xlApp.Visible = True
xlApp.UserControl = True
' Copy field names to the first row of the worksheet
fldCount = rst.Fields.Count
For iCol = 1 To fldCount
xlWs.Cells(1, iCol).Value = rst.Fields(iCol - 1).Name
Next
' Copy entire recordset to excel starting from A2
xlWs.Cells(2, 1).CopyFromRecordset rst
' Change the format
LineCount = rst.RecordCount
For iLine = 1 To LineCount
' xlWs.Cells(iLine + 1, 2).NumberFormat = "yyyymmdd"
xlWs.Cells(iLine + 1, 2).NumberFormat = "dd/mm/yyyy"
Next
' Auto-fit the column widths and row heights
xlApp.Selection.CurrentRegion.Columns.AutoFit
xlApp.Selection.CurrentRegion.Rows.AutoFit
' Save an Excel file from MS Access using VBA
xlApp.Application.ActiveWorkbook.SaveAs FileName:="c:\DEV\XL_test.xls"
'Close the recordset
rst.Close
' Quit Excel
xlApp.Quit
End Sub
|