将每个访问表导出到单个工作簿

PinkSmelly BlueSocks

我一直在使用这种语法将数据库中的每个表导出到一个excel工作簿,但是现在我需要将每个表导出到它自己的工作簿。如何将其调整为将每个表导出到其自己的工作簿?

Sub ExportToExcel()
  Dim td As DAO.TableDef, db As DAO.Database
  Dim out_file As String

  out_file = "C:\fromaccess.xlsx"

  Set db = CurrentDb()
    For Each td in db.TableDefs
      If Left(td.Name, 4) = "MSys" Then
        'Do Nothing
      Else
        DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, td.Name, out_file, True, Replace(td.Name, "dbo_","")
      End If
      Next
End Sub

编辑
我尝试了@ HA560的建议,但得到一个错误

运行时错误'91':
未设置对象变量或With块变量

这是更新的代码:

Sub ExportToExcel()
Dim td As DAO.TableDef, db As DAO.Database
Dim out_file As String
Dim xl As Excel.Application

out_file = "C:\fromaccess.xlsx"

Set db = CurrentDb()
For Each td in db.TableDefs
xl.Workbooks.Add  
If Left(td.Name, 4) = "MSys" Then
    'Do Nothing
  Else
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, td.Name, out_file, True, Replace(td.Name, "dbo_","")
  End If
  Next
End Sub
达伦·巴特鲁普·库克(Darren Bartrup-Cook)

一个长的位,其中包括三个过程。运行后,您应该在立即窗口中看到表名称和TRUE / FALSE的列表,说明导出是否成功。

ExportAll-主要程序。
CreateXL-这将创建一个Excel实例。它使用后期绑定,因此无需设置引用。

QueryExportToXL-这是导出表的代码。我没有用过,TransferSpreadsheet因为我喜欢更多的控制权。

  • 您需要将工作表引用传递给该函数。
  • 您可以将查询名称或记录集传递给该函数。
  • 您可以传递备用工作表名称。
  • 要粘贴的默认单元格是A1,但是您可以更改它。
  • 默认情况下,它会调整列宽以适合。
  • 您可以传递要使用的标题名称的集合,而不是字段名称。

那里没有太多错误处理-例如传递的标题名称与字段的数目不同,给出了非法的工作表名称。
它需要工作:)

Public Sub ExportAll()

    Dim db As DAO.Database
    Dim tdf As DAO.TableDef
    Dim rst As DAO.Recordset
    Dim oXL As Object
    Dim oWrkBk As Object

    Set db = CurrentDb

    'Create instance of Excel.
    Set oXL = CreateXL

    For Each tdf In db.TableDefs
        If Left(tdf.Name, 4) <> "MSys" Then

            'Create workbook with single sheet.
            Set oWrkBk = oXL.WorkBooks.Add(-4167) 'xlWBATWorksheet

            'Open the table recordset.
            Set rst = tdf.OpenRecordset

            'In the immediate window display table name and TRUE/FALSE if exported successfully.
            Debug.Print tdf.Name & " - " & QueryExportToXL(oWrkBk.worksheets(1), , rst, tdf.Name)

            'Save and close the workbook.
            oWrkBk.SaveAs "<path to folder>" & tdf.Name
            oWrkBk.Close

        End If
    Next tdf

End Sub

'----------------------------------------------------------------------------------
' Procedure : CreateXL
' Author    : Darren Bartrup-Cook
' Date      : 02/10/2014
' Purpose   : Creates an instance of Excel and passes the reference back.
'-----------------------------------------------------------------------------------
Public Function CreateXL(Optional bVisible As Boolean = True) As Object

    Dim oTmpXL As Object

    '''''''''''''''''''''''''''''''''''''''''''''''''''''
    'Defer error trapping in case Excel is not running. '
    '''''''''''''''''''''''''''''''''''''''''''''''''''''
    On Error Resume Next
    Set oTmpXL = GetObject(, "Excel.Application")

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'If an error occurs then create an instance of Excel. '
    'Reinstate error handling.                            '
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''
    If Err.Number <> 0 Then
        Err.Clear
        On Error GoTo ERROR_HANDLER
        Set oTmpXL = CreateObject("Excel.Application")
    End If

    oTmpXL.Visible = bVisible
    Set CreateXL = oTmpXL

    On Error GoTo 0
    Exit Function

ERROR_HANDLER:
    Select Case Err.Number

        Case Else
            MsgBox "Error " & Err.Number & vbCr & _
                " (" & Err.Description & ") in procedure CreateXL."
            Err.Clear
    End Select


End Function


'----------------------------------------------------------------------------------
' Procedure : QueryExportToXL
' Author    : Darren Bartrup-Cook
' Date      : 26/08/2014
' Purpose   : Exports a named query or recordset to Excel.
'-----------------------------------------------------------------------------------
Public Function QueryExportToXL(wrkSht As Object, Optional sQueryName As String, _
                                                  Optional rst As DAO.Recordset, _
                                                  Optional SheetName As String, _
                                                  Optional rStartCell As Object, _
                                                  Optional AutoFitCols As Boolean = True, _
                                                  Optional colHeadings As Collection) As Boolean

    Dim db As DAO.Database
    Dim prm As DAO.Parameter
    Dim qdf As DAO.QueryDef
    Dim fld As DAO.Field
    Dim oXLCell As Object
    Dim vHeading As Variant

    On Error GoTo ERROR_HANDLER

    If sQueryName <> "" And rst Is Nothing Then

        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        'Open the query recordset.                               '
        'Any parameters in the query need to be evaluated first. '
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Set db = CurrentDb
        Set qdf = db.QueryDefs(sQueryName)
        For Each prm In qdf.Parameters
            prm.Value = Eval(prm.Name)
        Next prm
        Set rst = qdf.OpenRecordset
    End If

    If rStartCell Is Nothing Then
        Set rStartCell = wrkSht.cells(1, 1)
    Else
        If rStartCell.Parent.Name <> wrkSht.Name Then
            Err.Raise 4000, , "Incorrect Start Cell parent."
        End If
    End If


    If Not rst.BOF And Not rst.EOF Then
        With wrkSht
            Set oXLCell = rStartCell

            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            'Paste the field names from the query into row 1 of the sheet. '
            'Or the alternative field names provided in a collection.      '
            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            If colHeadings Is Nothing Then
                For Each fld In rst.Fields
                    oXLCell.Value = fld.Name
                    Set oXLCell = oXLCell.Offset(, 1)
                Next fld
            Else
                For Each vHeading In colHeadings
                    oXLCell.Value = vHeading
                    Set oXLCell = oXLCell.Offset(, 1)
                Next vHeading
            End If

            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            'Paste the records from the query into row 2 of the sheet. '
            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            Set oXLCell = rStartCell.Offset(1, 0)
            oXLCell.copyfromrecordset rst
            If AutoFitCols Then
                .Columns.Autofit
            End If

            If SheetName <> "" Then
                .Name = SheetName
            End If

            '''''''''''''''''''''''''''''''''''''''''''
            'TO DO: Has recordset imported correctly? '
            '''''''''''''''''''''''''''''''''''''''''''
            QueryExportToXL = True

        End With
    Else

        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        'There are no records to export, so the export has failed. '
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        QueryExportToXL = False
    End If

    Set db = Nothing

    On Error GoTo 0
    Exit Function

ERROR_HANDLER:
    Select Case Err.Number

        Case Else
            MsgBox "Error " & Err.Number & vbCr & _
                " (" & Err.Description & ") in procedure QueryExportToXL."
            Err.Clear
            Resume
    End Select

End Function

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将数据导出到多个Excel工作表

来自分类Dev

用于将单个表导出到SQL文件的PHP脚本

来自分类Dev

将数据从VB导出到Excel工作表

来自分类Dev

将工作簿合并到主工作簿中,每个文件都有单独的工作表

来自分类Dev

将工作表导出到CSV

来自分类Dev

将数据透视表和源数据导出到另一个工作簿

来自分类Dev

如何将多个数据表导出到单个Excel文件中的多个工作表

来自分类Dev

如何仅将Excel中的一个工作表导出到单个htm文件?

来自分类Dev

SSRS将Tablix导出到不同的Excel工作表

来自分类Dev

使用格式和布局将多个表从Access DB导出到Excel工作簿

来自分类Dev

将工作表数据导出到JSON文件

来自分类Dev

将熊猫数据框导出到Excel工作表

来自分类Dev

导出到新工作簿

来自分类Dev

将数据从jqgrid导出到Excel工作表

来自分类Dev

用于将单个表导出到SQL文件的PHP脚本

来自分类Dev

将工作簿合并到主工作簿中,每个文件都有单独的工作表

来自分类Dev

将工作表导出到CSV

来自分类Dev

将某个工作表从工作簿导出为pdf

来自分类Dev

将数据导出到Excel工作表

来自分类Dev

将工作表以CSV格式导出到Worbook路径

来自分类Dev

如何通过每个子报表将SSRS报表导出到不同的Excel工作表?

来自分类Dev

如何将关闭的工作簿表导出到.CSV文件?

来自分类Dev

SSRS将Tablix导出到不同的Excel工作表

来自分类Dev

将Excel工作表范围导出到PDF错误

来自分类Dev

将多个结果导出到同一Excel工作表

来自分类Dev

将信息列导出到新工作表

来自分类Dev

VBA 宏 - 将表格数据从 Excel 文件导出到 Word 并为每个工作表创建一个部分

来自分类Dev

将每个电子表格导出到单独的工作簿(VBA 除外)

来自分类Dev

将多个工作簿合并到当前工作簿中的单个工作表中

Related 相关文章

  1. 1

    将数据导出到多个Excel工作表

  2. 2

    用于将单个表导出到SQL文件的PHP脚本

  3. 3

    将数据从VB导出到Excel工作表

  4. 4

    将工作簿合并到主工作簿中,每个文件都有单独的工作表

  5. 5

    将工作表导出到CSV

  6. 6

    将数据透视表和源数据导出到另一个工作簿

  7. 7

    如何将多个数据表导出到单个Excel文件中的多个工作表

  8. 8

    如何仅将Excel中的一个工作表导出到单个htm文件?

  9. 9

    SSRS将Tablix导出到不同的Excel工作表

  10. 10

    使用格式和布局将多个表从Access DB导出到Excel工作簿

  11. 11

    将工作表数据导出到JSON文件

  12. 12

    将熊猫数据框导出到Excel工作表

  13. 13

    导出到新工作簿

  14. 14

    将数据从jqgrid导出到Excel工作表

  15. 15

    用于将单个表导出到SQL文件的PHP脚本

  16. 16

    将工作簿合并到主工作簿中,每个文件都有单独的工作表

  17. 17

    将工作表导出到CSV

  18. 18

    将某个工作表从工作簿导出为pdf

  19. 19

    将数据导出到Excel工作表

  20. 20

    将工作表以CSV格式导出到Worbook路径

  21. 21

    如何通过每个子报表将SSRS报表导出到不同的Excel工作表?

  22. 22

    如何将关闭的工作簿表导出到.CSV文件?

  23. 23

    SSRS将Tablix导出到不同的Excel工作表

  24. 24

    将Excel工作表范围导出到PDF错误

  25. 25

    将多个结果导出到同一Excel工作表

  26. 26

    将信息列导出到新工作表

  27. 27

    VBA 宏 - 将表格数据从 Excel 文件导出到 Word 并为每个工作表创建一个部分

  28. 28

    将每个电子表格导出到单独的工作簿(VBA 除外)

  29. 29

    将多个工作簿合并到当前工作簿中的单个工作表中

热门标签

归档