0

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

任何帮助,将不胜感激。

克里斯

4

1 回答 1

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
于 2013-10-18T02:57:56.817 回答