【问题标题】:How to open an access workbook and transfer a table into an excel worksheet using the VBA macro ran on Excel?如何使用在 Excel 上运行的 VBA 宏打开访问工作簿并将表格传输到 Excel 工作表?
【发布时间】:2018-03-13 10:40:07
【问题描述】:

我读过从 Access 导出 Excel 工作表将使用 DoCmd 对象和 TransferSpreadsheet 方法,但是,我需要一个可以从我的 Excel VBA 脚本运行的宏。

所以步骤如下:

  1. 在 Excel 中运行宏
  2. 将访问表转换为 Excel 工作表
  3. Excel VBA 代码在新转换的工作表上运行脚本。

或者如果有办法

  1. 在 Excel 中运行宏
  2. 在访问表上执行脚本,效果更好。

【问题讨论】:

  • 目前尚不清楚您在问什么。您几乎可以在单个 VBA 宏中执行任何操作,无论是从 Excel 还是从 Access。
  • 您为什么不直接从 Access 导入 Excel:Import Access DataVBA Import from Access to Excel 在 www 中还有更多教程。因此,如果您需要一个宏,您将需要完成这些教程之一并编写一个。
  • 我知道如何手动将访问表导出到 Excel 工作表中。但是,我真正计划实现的是运行一个宏,该宏将通过单击 Excel VBA 代码将多个工作表一起编译。因为我们的员工正在处理多个 excel/access 文件,并将这些文件保存在云服务器上。我不想手动逐个导出,而是计划通过云中文件的路径检索每个文件
  • 然后问一个问题,并提供足够的细节让我能够写一个答案。
  • @Pᴇʜ 感谢这是解决方案的 95%。 5%可以通过录制宏并按刷新按钮很容易找到vba代码刷新。

标签: excel ms-access vba


【解决方案1】:

嗯,您的问题并不完全有道理,但我猜您想将数据从 Access 导入 Excel。对吗?

Import data from Access to Excel (DAO)

CopyFromRecordset is probably the easiest method of getting data from an Access table to an Excel worksheet.

Sub DAOCopyFromRecordSet(DBFullName As String, TableName As String, _
    FieldName As String, TargetRange As Range)
' Example: DAOCopyFromRecordSet "C:\FolderName\DataBaseName.mdb", _
    "TableName", "FieldName", Range("C1")
Dim db As Database, rs As Recordset
Dim intColIndex As Integer
    Set TargetRange = TargetRange.Cells(1, 1)
    Set db = OpenDatabase(DBFullName)
    Set rs = db.OpenRecordset(TableName, dbOpenTable) ' all records
    'Set rs = db.OpenRecordset("SELECT * FROM " & TableName & _
        " WHERE " & FieldName & _
        " = 'MyCriteria'", dbReadOnly) ' filter records
    ' write field names
    For intColIndex = 0 To rs.Fields.Count - 1
        TargetRange.Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name
    Next
    ' write recordset
    TargetRange.Offset(1, 0).CopyFromRecordset rs
    Set rs = Nothing
    db.Close
    Set db = Nothing
End Sub

If you want more control with the data import, you can customize the macro below:

Sub DAOFromAccessToExcel(DBFullName As String, TableName As String, _
    FieldName As String, TargetRange As Range)
' Example: DAOFromAccessToExcel "C:\FolderName\DataBaseName.mdb", _
    "TableName", "FieldName", Range("B1")
Dim db As Database, rs As Recordset
Dim lngRowIndex As Long
    Set TargetRange = TargetRange.Cells(1, 1)
    Set db = OpenDatabase(DBFullName)
    Set rs = db.OpenRecordset(TableName, dbOpenTable) ' all records
    'Set rs = DB.OpenRecordset("SELECT * FROM " & _
        TableName & " WHERE " & FieldName & _
        " = 'MyCriteria'", dbReadOnly) ' filter records
    lngRowIndex = 0
    With rs
        If Not .BOF Then .MoveFirst
        While Not .EOF
            TargetRange.Offset(lngRowIndex, 0).Formula = .Fields(FieldName)
            .MoveNext
            lngRowIndex = lngRowIndex + 1
        Wend
    End With
    Set rs = Nothing
    db.Close
    Set db = Nothing
End Sub

或者……

  Import data from Access to Excel (ADO)

With the procedure below you can import data from an Access table to a worksheet.

Sub ADOImportFromAccessTable(DBFullName As String, _
    TableName As String, TargetRange As Range)
' Example: ADOImportFromAccessTable "C:\FolderName\DataBaseName.mdb", _
    "TableName", Range("C1")
Dim cn As ADODB.Connection, rs As ADODB.Recordset, intColIndex As Integer
    Set TargetRange = TargetRange.Cells(1, 1)
    ' open the database
    Set cn = New ADODB.Connection
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
        DBFullName & ";"
    Set rs = New ADODB.Recordset
    With rs
        ' open the recordset
        .Open TableName, cn, adOpenStatic, adLockOptimistic, adCmdTable 
        ' all records
        '.Open "SELECT * FROM " & TableName & _
            " WHERE [FieldName] = 'MyCriteria'", cn, , , adCmdText 
        ' filter records

        RS2WS rs, TargetRange ' write data from the recordset to the worksheet

'        ' optional approach for Excel 2000 or later (RS2WS is not necessary)
'        For intColIndex = 0 To rs.Fields.Count - 1 ' the field names
'            TargetRange.Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name
'        Next
'        TargetRange.Offset(1, 0).CopyFromRecordset rs ' the recordset data

    End With
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
End Sub

【讨论】:

    猜你喜欢
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    • 2018-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多