I’m getting a “object variable or with block variable not set” error message when running the following code. It is in Access and refers to an Excel spreadsheet. What is wrong with the code?
wsTest.Range("A11").Activate
Do Until IsEmpty(ActiveCell)
intX = intX + wsTest.Cells(ActiveCell.Row, "L") 'error occurs on this line
intY = intY + wsTest.Cells(ActiveCell.Row, "I")
ActiveCell.Offset(1, 0).Select ' Step down 1 row to the next cell.
Loop
The first time the code is run, there is no error, only the second time. Closing and reopening Access “fixes” the problem. How can that be related to this code?
As the error occurs in the highlighted line, I believe you’re trying to do an invalid sum. Maybe the value returned by
wsTest.Cells(oCell.Row, "L")isn’t a integer, or you should usewsTest.Cells(oCell.Row, "L").Valueinstead.Will be easier to identify the root cause of the problem adding a msgbox to check the cell value, as I added below.
Either way, I’d suggest you to avoid using references to ActiveCell, as they aren’t reliable. Maybe this code helps avoiding this error (simply replacing the ActiveCell by a proper cell object).