2013-10-17 99 views
0

我有一個數據庫可以輸出4個報告給Excel。這些都在不同的工作簿中。 但是我想將這些全部輸出到一個模板工作簿中,但在不同的工作表中。通過VBA輸出訪問數據到多個Excel工作表

任何幫助,將不勝感激。

克里斯

+2

是否有現有代碼將報告輸出到Excel?如果是這樣,請[編輯](http://stackoverflow.com/posts/19431037/edit)顯示它的問題。 –

回答

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 
相關問題