如何在从ms访问导出过程之前更改excel列的属性?

时间:2013-08-29 10:00:01

标签: sql excel vba ms-access properties

我目前正在通过点击按钮将ms访问过程导出到excel。但是,我想更改日期列的属性,将其显示为“dd / mm / yyy hh:mm”类型,而不是默认的“dd-mm-yy”。在vba访问代码中有没有办法做到这一点?感谢

1 个答案:

答案 0 :(得分: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实现最大兼容性。