通过VBA将Access数据输出到多个Excel工作表

时间:2013-10-17 15:32:41

标签: excel ms-access access-vba ms-access-2013

我有一个数据库,可以向Excel输出4个报告。这些都在不同的工作簿中。 但是,我想将这些全部输出到一个模板工作簿,但是在不同的工作表中。

任何帮助将不胜感激。

克里斯

1 个答案:

答案 0 :(得分:0)

你走了。我在这里使用后期绑定。

Private Sub OutputToExcel()
    Dim oExcel As Object
    Dim oBook As Object
    Dim oSheet As Object

    On Error Resume Next
    Set oExcel = GetObject(, "Excel.Application")
    If Err.Number <> 0 Then
        Err.Clear
        Set oExcel = CreateObject("Excel.Application")
    End If
    On Error GoTo 0

    oExcel.Visible = True
    Set oBook = oExcel.Workbooks.Add 'default workbook has 3 sheets
    Set oSheet = oBook.worksheets(1)
    oSheet.Range("A1").Value = "Test " & oSheet.Name
    Set oSheet = oBook.worksheets(2)
    oSheet.Range("A1").Value = "Test " & oSheet.Name
    Set oSheet = oBook.worksheets(3)
    oSheet.Range("A1").Value = "Test " & oSheet.Name
    Set oSheet = oBook.worksheets.Add
    oSheet.Name = "Test Sheet4"
    oSheet.Range("A1").Value = "Test " & oSheet.Name

    Set oSheet = Nothing
    Set oBook = Nothing
    Set oExcel = Nothing
End Sub