什么是“编译错误用户定义的类型未定义”?

时间:2015-09-11 14:35:29

标签: vba ms-access-2007 excel-2007

我正在尝试将工作表从Excel 2007导出到Windows 7上的Access 2007,包括创建新表。我在Excel中尝试了以下VBA,但它指出“编译错误用户定义的类型未定义”,然后突出显示Access对象的第一个变量声明。我引用了Microsoft ActiveX Data Objects 6.1库。

有什么问题?

Dim acc As New Access.Application
Dim ws As Worksheet
Dim ThisWorkbook As Workbook
Dim a As Long
Dim b As Long

Set ThisWorkbook = ActiveWorkbook
Set ws = ThisWorkbook.Sheets("CompleteNoFormat3")
a = ws.UsedRange.Rows.Count
b = ws.UsedRange.Columns.Count

acc.OpenCurrentDatabase "CustDB.mdb"
  acc.DoCmd.TransferSpreadsheet _
   TransferType:=acImport, _
   SpreadSheetType:=acSpreadsheetTypeExcel12Xml, _
   TableName:="CustExportTable", _
   Filename:=Application.ActiveWorkbook.FullName, _
   HasFieldNames:=True, _
   Range:="BAPCompletNoFormat$A1:" & a & B 
acc.CloseCurrentDatabase
acc.Quit
Set acc = Nothing

1 个答案:

答案 0 :(得分:1)

考虑使用后期绑定,有时甚至建议将应用程序分发给可能安装了不同库版本类型的多个用户。

后期绑定避免了需要专门选择引用作为CPU搜索适当类型和不support the "New" keyword的应用程序:

Dim acc As Object

Set acc = CreateObject("Access.Application")