将两个Access表导出到同一个Excel选项卡

时间:2015-10-02 15:38:42

标签: excel-vba ms-access vba excel

我想知道是否可以将两个访问表导出到同一个excel选项卡而不仅仅是工作表。我想在同一页面上加入两个表格。有人知道如何做到这一点或任何其他可能有帮助的解决方案吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

如果表之间存在关系,则可以在访问中创建查询,然后导出查询

答案 1 :(得分:0)

由于表不相关,因此请在Access VBA CopyFromRecordset中使用表格输出到特定范围的Excel工作表。您将只需要引用最左上角的单元格(因此请确保考虑表格的列和行不重叠数据):

Public Sub acToxlRecordsets()

    Dim xlApp As Object, xlwkb As Object
    Dim db As Database
    Dim frst As Recordset, srst As Recordset    
    Dim strPath As String

    Set db = CurrentDb()
    Set xlApp = CreateObject("Excel.Application")

    strPath = "C:\Path\To\Excel\Workbook.xlsx"

    Set xlwkb = xlApp.Workbooks.Open(strPath)

    ' OPEN FIRST RECORDSET
    Set frst = db.OpenRecordset("FirstTable", dbOpenDynaset)
    frst.MoveFirst

    ' OUTPUT RECORDSET (SELECT TOP LEFT CORNER --CHANGE SHEET AS NEEDED)         
    xlwkb.Worksheets(1).Range("A1").CopyfromRecordset frst
    frst.Close

    ' OPEN SECOND RECORDSET
    Set frst = db.OpenRecordset("SecondTable", dbOpenDynaset)
    srst.MoveFirst

    ' OUTPUT RECORDSET (SELECT TOP LEFT CORNER --CHANGE SHEET AS NEEDED)        
    xlwkb.Worksheets(1).Range("J1").CopyfromRecordset srst
    srst.Close

    ' SAVE AND CLOSE EXCEL WORKBOOK
    xlwkb.Close True
    xlApp.Quit

    Set xlwkb = Nothing
    Set xlApp = Nothing
    Set frst = Nothing
    Set srst = Nothing
    Set db = Nothing   

End Sub