简体   繁体   English

Access / Excel:方法对象结束范围失败?

[英]Access/Excel: Method End of object Range failed?

I have an Access db that processes and imports Excel workbooks. 我有一个Access db,用于处理和导入Excel工作簿。 It has historically worked well, but is suddenly giving me this error on all workbooks - even ones that previously worked: 从历史上讲它运行良好,但是突然在所有工作簿上给我这个错误-甚至以前的工作簿也是如此:

-2147417851: Method 'End' of object 'Range' failed 

The line causing the problem is: 导致问题的行是:

iLastCBRow = XlBook.Worksheets("4_CensusBlocks").Range("B16001").End(xlUp).Row

If I step through this line in the Immediate window and enter 如果我在立即窗口中浏览此行并输入

XlBook.Worksheets("4_CensusBlocks").Range("B16001").Value

it returns the value of that cell correctly. 它正确返回该单元格的值。

I thought that the Access db might be corrupt, so I recreated it. 我认为Access数据库可能已损坏,所以我重新创建了它。 Same problem. 同样的问题。 I also manually forced a repair on the Excel workbook. 我还手动对Excel工作簿进行了强制修复。 I even uninstalled and reinstalled Office. 我什至卸载并重新安装了Office。

Running Office 2016 64 bit now on Windows 7. 现在在Windows 7上运行64位Office 2016。

Does anyone have an idea what the problem might be? 有谁知道可能是什么问题? Thanks 谢谢

it looks like it is because you have more than 16001 lines in the file. 看起来是因为文件中的行数超过了16001。 The first command 第一条命令

iLastCBRow = XlBook.Worksheets("4_CensusBlocks").Range("B16001").End(xlUp).Row)

sets iLastCBRow to the starting row. 将iLastCBRow设置为起始行。

Try using 尝试使用

iLastCBRow = ActiveSheet.UsedRange.SpecialCells(xlLastCell).Row

instead 代替

I still don't understand WHY, but the problem seems to have been in using early binding of Excel object variables. 我仍然不明白为什么,但是问题似乎出在使用Excel对象变量的早期绑定。 Once I put in place 一旦我就位

dim xl as object 
dim xlbook as object
set xl = createobject("Excel.Application")
set xlbook = xl.workbooks.open(filename)

Instead of just 不只是

dim xlbook as excel.workbook
set xlbook = getobject(filename)

The error went away. 错误消失了。 I have only had Office 2016 installed this whole time, so perhaps an update made the OLE server think that I had multiple Offices installed or something?? 我整个时间只安装了Office 2016,所以也许更新使OLE服务器认为我安装了多个Office或其他东西? No idea. 不知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM