从Access导出到Excel

时间:2012-01-18 20:17:42

标签: excel ms-access vba

我需要Access使用VBA将信息导出到Excel,格式如下:

  1. 抓住一行和与该行相关的某些列
  2. 在Excel中吐出
  3. 为Excel中的每一行创建新工作表时为每一行循环该过程
  4. 更具体地说,每一行将是一个不同的位置(例如:dallas,chicago ......),我只想从每个位置提取某些信息,并为每个位置创建一个电子表格。

2 个答案:

答案 0 :(得分: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 :(得分: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