我目前正在通过单击按钮将程序从 ms 访问导出到 excel。但是,我想更改日期列的属性以将其显示为类型“dd/mm/yyy hh:mm”,而不是默认的“dd-mm-yy”。有没有办法在 vba 访问代码中执行此操作?谢谢
问问题
450 次
1 回答
3
'<< Your existing code to export query to Excel >>
Dim xl As Object 'the Excel Application
On Error Resume Next
'Attempt to use an existing instance of Excel
Set xl = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
On Error Goto 0 'Restore appropriate ErrorHandler here
'Create new instance of Excel
Set xl = CreateObject("Excel.Application")
Else
On Error Goto 0 'Restore appropriate ErrorHandler here
End If
Dim wb As Object 'the Excel Workbook object
Set wb = xl.Workbooks.Open(FullPathToExcelWorkbook)
Dim ws As Object 'the Excel Worksheet object
Set ws = wb.Worksheets(1)
Dim col As Object 'the Column whose data type you want to change
Dim FieldName As String 'the name of the query's field to change
FieldName = "MyDateAndTimeColumn"
Set col = ws.Columns(ws.Cells.Find(FieldName).Column)
col.NumberFormat = "dd/mm/yyyy hh:mm"
您显然需要添加适当的错误处理(例如,Excel 文件可能已经打开,等等),但这应该可以帮助您。另外,我在这里使用了后期绑定,以最大程度地兼容不同版本的 Excel。
于 2013-08-29T13:05:37.953 回答