检查MS Access中是否存在命名范围

我在Access中使用下面的VBA在Excel中导入一个名为“Drop”的范围,但是,如果范围Drop存在于Excel中,如何首先检入Access:

DoCmd.TransferSpreadsheet , acSpreadsheetTypeExcel12, "tblCustomerImportDrop", ExcelFilePath, True, "Drop" 

或者,有没有办法抑制错误3011(当它无法find范围时产生),并继续我的代码?

在VBA中抑制错误非常简单:

只需On Error Resume Next导致错误的行之前添加On Error Resume Next ,然后On Error GoTo 0其后面添加On Error GoTo 0 ,以查看所有其他行的错误。

检查Excel中是否存在范围可以完成,但要求您具有Microsoft Excel对象库,打开Excel应用程序,然后closures它,除非通过错误捕获完成(我可以向您显示错误捕获和非错误诱捕方法如果感兴趣)。

  Sub usage_example() fpath = "d:\names_test.xlsx" findedname = "xxxx" ret = existname(fpath, findedname) End Sub Function existname(fpath, findedname) As Boolean Set xlapp = CreateObject("Excel.application") Set wb = xlapp.workbooks.Open(fpath) existname = False For Each nn In wb.names If nn.Name = findedname Then existname = True Exit For End If Next nn wb.Close Set wb = Nothing Set xlapp = Nothing End Function