导入Excel数据以通过Excel VBA访问

导入Excel数据以通过Excel VBA访问,vba,excel,Vba,Excel,当我尝试导出数据时,出现以下错误: 错误3265。在集合中找不到项 我无法导出数据,因为行中有错误 Sub AccImport() Dim dbConnection As ADODB.Connection Dim dbFileName As String Dim dbRecordset As ADODB.Recordset Dim xRow As Long, xColumn As Long Dim LastRow As Long 'Go to t

当我尝试导出数据时,出现以下错误:

错误3265。在集合中找不到项

我无法导出数据,因为行中有错误

Sub AccImport()
    Dim dbConnection As ADODB.Connection
    Dim dbFileName As String
    Dim dbRecordset As ADODB.Recordset
    Dim xRow As Long, xColumn As Long
    Dim LastRow As Long

    'Go to the worksheet containing the records you want to transfer.
    Worksheets("Completed").Activate

    'Determine the last row of data based on column A.
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row

    'Create the connection to the database.
    Set dbConnection = New ADODB.Connection

    'Define the database file name
    dbFileName = "C:/..."

    'Define the Provider and open the connection.
    With dbConnection
        .Provider = "Microsoft.ACE.OLEDB.12.0;Data Source=" & dbFileName & _
                    ";Persist Security Info=False;"
        .Open dbFileName
    End With

    'Create the recordset
    Set dbRecordset = New ADODB.Recordset

    dbRecordset.CursorLocation = adUseServer
    dbRecordset.Open Source:="Resolution", _
                              ActiveConnection:=dbConnection, _
                              CursorType:=adOpenDynamic, _
                              LockType:=adLockOptimistic, _
                              Options:=adCmdTable

    'Loop thru rows & columns to load records from Excel to Access.
    'Assume row 1 is the header row, so start at row 2.
    For xRow = 2 To LastRow
        dbRecordset.AddNew
        'Assume this is an 26-column (field) table starting with column A.
        For xColumn = 1 To 26
            dbRecordset(Cells(1, xColumn).Value) = Cells(xRow, xColumn).Value
        Next xColumn
        dbRecordset.Update
    Next xRow

    'Close the connections.
    dbRecordset.Close
    dbConnection.Close

    'Release Object variable memory.
    Set dbRecordset = Nothing
    Set dbConnection = Nothing
    'Optional:
    'Clear the range of data (the records) you just transferred.
    Range("A2:Z" & LastRow).ClearContents
 End Sub

。。。任何想法都可以

而不是那样做。使用
docmd.transferSpreadsheet

一行将数据导入Access。您需要添加参考
Microsoft Access 14.0对象库
,才能使用
docmd
。或者直接从Access运行它

因此,它将类似于
docmd.TransferSpreadsheet acExport,acSpreadsheetTypeExcel12,“AccessTableName”,“FullFileName”,true,“Range”

将,
AccessTablename
替换为要导入到的表的名称。
FullFileName
带有excel工作表的完整文件名<代码>范围使用范围excel工作表。如果只想从Excel中复制所有内容,可以删除范围

编辑。更新为开放存取

dbRecordset(Cells(1, xColumn).Value) = Cells(xRow, xColumn).Value

这将打开Access DB。运行代码并导入表

Sam我想导出一张工作表的所有数据,不包括页眉。那么在这种情况下,范围是多少。。你能帮我一下吗?如果你不想让领头的话,把
true
改成
false
。更改为不包含标题行的错误传递电子表格现在不可用。我现在有什么要做的吗?我已经检查了所有可用的参考资料查看时,您可能必须首先通过VBA打开Access数据库。我将调查此事,并在完成调查后更新我的答案。。谢谢你,山姆。。将等待您的答复。
Dim appAccess As Access.Application
    Set appAccess = CreateObject("Access.Application")

    appAccess.Visible = True

    appAccess.OpenCurrentDatabase "FILENAME OF ACCESS DB"

    appAccess.docmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "AccessTableName", "FullFileName", False