我是新手,我在使用 vba 代码从 excel 文件访问中的表中导入数据时遇到问题。使用 Office 2010,但代码的启动给了我一个错误:(从意大利语翻译)运行时错误 -2147217865 (80040e37) Microsoft Access 数据库引擎找不到对象'INCONTRI$A1:I108468。确保对象存在并且其名称和您键入的路径已更正。
这是代码:
VB:
Sub EXPORT2ACCESS()
Dim ultimariga As Long
Dim ultimacolonna As Integer
Dim foglio, finesel As String
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\TOTALBET\TOTALBET_XML_DB.accdb"
dbWb = Application.ActiveWorkbook.FullName
For i = 1 To 4
ultimariga = Sheets(i).Range("A" & Rows.Count).End(xlUp).Row
ultimacolonna = Sheets(i).Cells(1, Sheets(i).Columns.Count).End(xlToLeft).Column
finesel = Application.ConvertFormula("R" & ultimariga & "C" & ultimacolonna, xlR1C1, xlA1, toAbsolute:=xlRelative)
foglio = Application.Sheets(i).Name
Set cn = CreateObject("ADODB.Connection")
Dim cmd As ADODB.Command
dsh = "[" & Application.Sheets(i).Name & "$A1:" & finesel & "]"
cn.Open strCon
Set cmd = New ADODB.Command
cmd.CommandType = adCmdText
cmd.CommandText = "Delete * from " & foglio
cmd.ActiveConnection = cn.ConnectionString
cmd.Execute
With Sheets(i)
For x = 1 To ultimacolonna
campo = "[" & .Cells(1, x).Value & "]"
If x = 1 Then
stringa = campo
Else
stringa = stringa & ", " & campo
End If
Next x
End With
''Insert into a table called
strsql = "INSERT INTO " & foglio & "(" & stringa & ") "
strsql = strsql & "SELECT * FROM [Excel 12.0;HDR=YES;DATABASE=" & dbWb & "]." & dsh
Debug.Print strsql
''Execute the statement
cn.Execute strsql
Next i
End Sub
表“incontri”存在于我的 excel 文件中并包含数据。相同的子程序适用于其他工作表而没有问题,并在访问表中插入数据。
传递给执行的字符串是:
VB:
INSERT INTO INCONTRI([Static_ID], [Stage_ID], [Season], [Number], [Data], [Time], [Status], [Venue_ID], [League]) SELECT * FROM [Excel 12.0;HDR=YES;DATABASE=C:\Users\Admin\Desktop\test xml.xlsm].[INCONTRI$A1:I108468]
这是文件(太大无法放入论坛,抱歉:
带有代码和数据的 Excel 文件:https ://www.sugarsync.com/pf/D9350024_63096411_117675
访问文件:https ://www.sugarsync.com/pf/D9350024_63096411_117635
我要疯了才能理解错误在哪里,请帮忙!提前感谢所有想帮助我找出我错在哪里的人。问候文森特