0

現在、ボタンをクリックするだけで、ms access から Excel に手順をエクスポートしています。ただし、日付列のプロパティを変更して、デフォルトの「dd-mm-yy」ではなく「dd/mm/yyy hh:mm」タイプとして表示したいと考えています。アクセスのvbaコード内でこれを行う方法はありますか? ありがとう

4

1 に答える 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 に答える