我发现过类似的问题,但我似乎无法弄清楚使用这些例子。Excel VBA导入数据到现有的Access数据库
我在Excel中写了一个宏,最后一步需要将Sheet1中的所有数据导入A:Q(第1行中的标题)到Access数据库中的Table1。我需要确保Excel中的每个列对应于Access中的正确字段。
我使用Excel 2010/2013和Access 2010/2013。我目前使用的代码是:
Sub AccImport()
Dim acc As New Access.Application
acc.OpenCurrentDatabase "C:\Users\Benjamin\Documents\Database1.accdb"
acc.DoCmd.TransferSpreadsheet _
acImport, _
acSpreadsheetTypeExcel12Xml, _
"Sheet1", _
Application.ActiveWorkbook.FullName, _
True, _
"A1:Q1000" 'Need to change to reflect the last row in the sheet
acc.CloseCurrentDatabase
acc.Quit
Set acc = Nothing
End Sub
的代码导致错误信息,突出acc As New Access.Application
并显示“编译错误 - 未定义的用户定义类型”
任何帮助将不胜感激。谢谢!
你试过了什么代码?你有没有得到任何错误? – thunderblaster
我想我需要使用一些这种语法 - http://msdn.microsoft.com/en-us/library/office/ff844793%28v=office.14%29.aspx,也许代码从这里http:///www.ozgrid.com/forum/showthread.php?t=76110。如果你能指向我一些代码,我认为我适应它,我只是没有找到我可以适应的东西。 @thunderblaster – blahblahblah
我曾尝试过这种方式,但遇到了一个错误:'Dim acc As New Access.Application acc.OpenCurrentDatabase“C:\ Users \ Benjamin \ Documents \ Database1.accdb” acc.DoCmd.TransferSpreadsheet _ acImport,_ acSpreadsheetTypeExcel12Xml,_ “tblExcelImport”,_ Application.ActiveWorkbook.FullName,_ 诚然,_ “CallLog” acc.CloseCurrentDatabase acc.Quit 设置ACC = Nothing' – blahblahblah