2012-01-18 209 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