简体   繁体   中英

Access/Excel: Method End of object Range failed?

I have an Access db that processes and imports Excel workbooks. 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. Same problem. I also manually forced a repair on the Excel workbook. I even uninstalled and reinstalled Office.

Running Office 2016 64 bit now on Windows 7.

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. The first command

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

sets iLastCBRow to the starting row.

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. 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?? No idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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