如何在从ms访问导出过程之前更改excel列的属性?
我目前正在导出一个程序从ms访问excel与点击一个button。 不过,我想更改date列的属性,将其显示为types“dd / mm / yyy hh:mm”,而不是默认的“dd-mm-yy”。 在vba代码中有没有办法做到这一点? 谢谢
'<< 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"
您显然需要添加适当的error handling(例如,Excel文件可能已经打开等),但这应该让你去。 另外,我在这里使用了后期绑定,以便与不同版本的Excel最大程度地兼容。