将excel表数据从excel vba传输到访问表,给出错误

时间:2014-04-04 17:56:33

标签: vba excel-vba excel

我正在尝试将Excel工作表数据传输到访问表。下面的代码会抛出错误变量而不是 并指向 acImport 。 TransferSpreadsheet方法适用于excel?任何替代方法都是将表单数据转移到仅从excel访问。

   Private Sub Cal_WEM_Click()
   Dim appAccess As Object
   Dim aPath, aDbase, aDSource, aTable, exePath As String
   Dim fileParam As String
   aPath = ActiveWorkbook.Path
   aDbase = "Linear.accdb"
   aDSource = aPath & "\" & aDbase

  Set appAccess = CreateObject("Access.Application")
  appAccess.Visible = True

  appAccess.OpenCurrentDatabase aDSource

 appAccess.DoCmd.TransferSpreadsheet transfertype:=acImport, _
        tablename:="yorno", _
        FileName:=aPath, Hasfieldnames:=True, _
        Range:="WEM!D:E", SpreadsheetType:=5
        appAccess.DoCmd.OpenForm "Input_Form_WEM"

 Application.DisplayAlerts = False
 ThisWorkbook.Saved = True
 Application.Quit
 Exit Sub
 End Sub

1 个答案:

答案 0 :(得分:0)

通过声明像这样的变量

来解决问题
 Const acImport = 0
 Const acSpreadsheetTypeExcel9 = 8

 appAccess.DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
"wemonoff1", ename, True, "WEM-ONOFF!D:E"