2012-01-18 202 views
0

我需要訪問使用VBA信息導出到Excel具有以下格式:從Access導出到Excel

  1. 搶行,與該行
  2. 吐出來在Excel
  3. 某些列在Excel中爲每一行創建一個新工作表的同時循環該過程

更具體地說,每一行都將是一個不同的位置(例如:dallas,chicago ...),我只是想拉一定來自每個位置的信息併爲每個位置創建一個電子表格。

+0

對不起...所以基於從我的數據,我想對每個小區創建新的片材(1)-a(x)和所有相關的每個這些細胞的相應的數據,全部同時將數據重新排列以匹配我已創建的模板。 – trahm2 2012-01-18 22:15:04

回答

1

你不會提供關於你的需求的很多信息,所以這只是一個介紹。如果您願意,我可以提供更多信息。

在Access VBA編輯器中,選擇Tools,然後選擇References。向下滾動至Microsoft Excel 11.0 Object Library,並通過單擊其中的方框將其選中。

骨架的所需的代碼:

Dim Path As String 
Dim xlApp As Excel.Application 
Dim xlWB As Excel.Workbook 

' I hold my Excel file in the same folder asmy Access database. 
' This gets me the name of the folder holding my database. 
Path = Application.CurrentProject.Path 

' I assume the Excel file already exists 
DestName = Path & "\" & "xxxxxxxx.xls" 

Set xlApp = New Excel.Application 
With xlApp 
    .Visible = True   ' This slows your macro but helps during debugging 
    '.Visible = False 
    Set xlWB = .Workbooks.Open(DestName) 
    With xlWB 
    With .Sheets("Sheet1") 
     ' Intro to syntax 
     ' * .Cells(Row,Column) gives access to any cell within the sheet. 
     ' * .Cells(Row,Column).Value gives read/write access to the value. 
     ' * .Cells(Row,Column).Font.Bold = True sets the cell to bold. 
     ' * RowLast = .Cells(Row.Count,"A").End(xlUp).Row get the number of the 
     ' last used row in column A. 
     .Cells(1, 1).Value = "A" 

     ' More statements here 

    End With 
    .Save   ' Save updated workbook to disc 
    .Close   ' Close workbook 
    End With 
    Set xlWB = Nothing ' Clear reference to workbook 
    .Quit   ' Exit Excel 
End With 
Set xlApp = Nothing  ' Clear reference to Excel 
+1

我想我會傾向於使用傳輸電子表格和相同的工作簿名稱運行一系列查詢。一旦查詢具有不同的名稱(芝加哥等),它們將被放置在一張新的工作表或同名的現有工作表中,並且這樣的查詢將會對未來的維護更容易。 – Fionnuala 2012-01-18 21:49:07

+0

即時對不起... 所以基於我的數據我想爲每個單元格創建一個新的工作表a(1)-a(x),以及所有與這些單元格相關的所有相應數據,被重新安排來匹配我已經創建的模板。 – trahm2 2012-01-18 22:03:00

+0

http://stackoverflow.com/questions/8918483/how-do-i-export-access-to-excel – trahm2 2012-01-18 22:30:17

0

「,這個代碼選擇數據的特定行到臨時訪問表,然後導出臨時表到Excel電子表格,則丟棄該臨時訪問表。

Private Sub btnXLS_Click() 

    Dim db As Database 
    Set db = CurrentDb() 

    db.Execute "select * into TempTbl from SourceTable where Fieldname = " & Values & "" 

    Dim outputFileName As String 
    outputFileName = "C:filename_" & Format(Date, "yyyyMMdd") & ".xls" 
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "TempTbl", outputFileName, True 

    On Error Resume Next 
     db.Execute "DROP TABLE TempTbl" 

    Set db = Nothing 

End Sub