I wrote a VBA macro in Excel quite a while back that was working flawlessly for months. Suddenly, the other day it started throwing an error, though I had not made any changes to the workbook:
Run-time error '-2147417848 (80010108)':
Automation error
The object invoked has disconnected from its clients.
I traced the error to the following lines of code.
wkbTool.Worksheets("Excl Spine").Range("A2:D" & exclRow).ClearContents '<--- RUNS GREAT
wkbTool.Worksheets("Delq Spine").Range("C2:D13").ClearContents '<--- ERROR OCCURS HERE
Note that the first line runs fine every time and the second line is where the error occurs. I point this out because they are doing roughly the same types of work, but one throws the error while the other doesn't. This isn't my first reference to wkbTool.Worksheets("Delq Spine")
in the script.
I'm aware that solutions for this error abound. I've tried most of them and a few ideas of my own. For example, the following throws the same error:
Attempt 1 - Everything is a variable
Dim wkbTool as Workbook
Dim wksht as Worksheet
Dim myRange as Range
Set wkbTool = Workbooks("Name.xlsm")
Set wksht = wkbTool.Worksheets("Delq Spine")
Set myRange = wksht.Range("C2:D13")
myRange.ClearContents '<--- ERROR
Attempt 2 - The opposite
Workbooks("Name.xlsm").Worksheets("Delq Spine").Range("C2:D13").ClearContents
Attempt 3 - Perhaps ClearContents
doing something crazy, skip it
For i = 3 to 4
For j = 2 to 13
wkbTool.Worksheets("Delq Spine").cells(j,i) = ""
Next j
Next i
This results in Excel crashing, though it does not throw the error first. It clearly performs the operation in the first iteration before crashing because, when Excel recovers the workbook, cell(2,3) is empty. Also, I am not using ClearContents
for the first line in this attempt, just in case ClearContents
is causing something in itself.
Attempt 4 - Fine, I don't need that line anyway
I comment out the line that is causing the error. Upon looping back the next time, there is a reference to Worksheets("Delq Spine")
that works in the first iteration but not the second:
With wkbTool.Worksheets("Delq Spine")
.Activate
.Range("C2").FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-1],'[" & wkbNew.Name & "]" & wksht.Name & "'!R3C2:R14C3,2,FALSE),0)"
End With
In the above, the worksheet is activated, but Excel crashes similar to Attempt 3 when assigning the formula to C2. Again, once Excel recovers, it's clear that the operation was successful, as the formula is updated in that cell.
Attempt 5 - Surely the workbook has been corrupted
I went back through my company's backup files to find a version of the workbook that was definitely working at the time. It ran in to the same error at the same place.
Attempt 6 - Let's just recreate the workbook then
I recreated the workbook from scratch, including writing in all formulas and formatting the cells. I did copy/paste the VBA script to the new version because I don't have time to rewrite it all only for it to fail again.
I will continue to add more information about attempts I made as I think of them, I've been working on this problem on and off for the last week.