将数据从Excel导出到Access – 错误:参数不可选

我试图从EXCEL 2010导出数据到Access 2010与我的Excel文件上的VBA代码,当我按下button。 我只是想将数据从“水质”表导出到我的数据库的“水质”表(在Excel文件中,访问文件是其他表格和表格)。

我的实际代码是:

Sub Button14_Click() ' Exports data from the active worksheet to a table in an Access database Dim cn As ADODB.Connection Dim rs As ADODB.Recordset Dim r As Long Dim LastRow As Long ' Set cn = New ADODB.Connection 'cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; " & _ "Data Source=C:\Documents and Settings\Administrador\Mis documentos\MonEAU\modelEAU Database V.2.accdb; " & _ "Persist Security Info=False;" strCon = "Provider=Microsoft.ACE.OLEDB.12.0; " & _ "Data Source=C:\Documents and Settings\Administrador\Mis documentos\MonEAU\modelEAU Database V.2.accdb" ' Late binding, so no reference is needed Set cn = CreateObject("ADODB.Connection") cn.Open strCon ' Find LastRow in Col A into the Sheet1 LastRow = Sheet1.Range("A" & Rows.Count).End(xlUp).Row ' Insert unto a table called Water_Quality scn = "[Excel 8.0;HDR=YES;DATABASE=" & ActiveWorkbook.FullName & "]" strSQL = "INSERT INTO Water_Quality " _ & "SELECT * FROM " & scn & ".[Sheet1$A5:L" & LastRow & "]" ' Execute the statement cn.Execute strSQL rs.Close cn.Close Set rs = Nothing Set cn = Nothing End Sub 

我可以debugging没有任何问题的代码,但是当我运行它时,会出现运行错误:“Microsoft Office Access数据库引擎找不到对象'Sheet1 $ A5:L10'。确保对象存在,并且拼写其名称和正确的path名称“。 看起来这行cn.Execute strSQL有问题。

我也检查了名称和path名称,我找不到问题所在。

任何帮助解决它将不胜感激。

以下是一次插入所有数据的示例:

 strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=z:\docs\test.accdb" ''Late binding, so no reference is needed Set cn = CreateObject("ADODB.Connection") cn.Open strCon ''Create a table called ATable scn = "[Excel 8.0;HDR=YES;DATABASE=" & ActiveWorkbook.FullName & "]" strSQL = "SELECT * INTO ATable " _ & "FROM " & scn & ".[Sheet7$A1:C4]" ''Execute the statement cn.Execute strSQL ''Insert into a table called ATable scn = "[Excel 8.0;HDR=YES;DATABASE=" & ActiveWorkbook.FullName & "]" strSQL = "INSERT INTO ATable " _ & "SELECT * FROM " & scn & ".[Sheet7$A1:C4]" ''Execute the statement cn.Execute strSQL ''Insert into a table with no column header in Excel, ''the fields are [afield],[atext],[another] scn = "[Excel 8.0;HDR=NO;DATABASE=" & ActiveWorkbook.FullName & "]" strSQL = "INSERT INTO ATable ([afield],[atext],[another]) " _ & "SELECT F1 As afield, F2 As AText, F3 As another FROM " _ & scn & ".[Sheet7$A1:C4]" ''Execute the statement cn.Execute strSQL