簡體   English   中英

從Access導出到Excel

[英]Exporting from Access to Excel

我需要使用以下格式使用VBA將信息導出到Excel的訪問權限:

  1. 抓取一行以及與該行相關的某些列
  2. 在Excel中吐出來
  3. 在Excel中為每一行創建新工作表的同時為每一行循環該過程

更具體地說,每一行將位於不同的位置(例如:達拉斯,芝加哥...),我只想從每個位置提取某些信息並為每個位置創建一個電子表格。

您沒有提供太多有關您的需求的信息,因此這只是一個介紹。 如果您願意,我可以提供更多信息。

在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

'此代碼將數據的特定行選擇到臨時訪問表中,然后將臨時表導出到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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM