The creation and customization of database applications using Microsoft Access
The error is a classic Excel Automation issue: the code calls Excel objects without fully qualifying them, which can leave hidden references and cause run-time error 1004 when the automation runs on newer environments.
In the failing line:
Set RangeMatrixA = AnyWorkSheet.Range(AnyWorkSheet.Cells(1), _
AnyWorkSheet.Cells(MatDim, MatDim)).Cells
Cells is used without being explicitly tied to the worksheet object inside the Range call. This is the pattern described where Visual Basic creates an implicit reference and later fails.
To fix it, qualify all Cells references with the worksheet object, as shown in the documented workaround. For example, change the line to:
Set RangeMatrixA = AnyWorkSheet.Range( _
AnyWorkSheet.Cells(1, 1), _
AnyWorkSheet.Cells(MatDim, MatDim) _
).Cells
Similarly, ensure every call to Excel objects, methods, or properties in the routine is fully qualified with the appropriate object variable (such as AnyWorkSheet, RangeMatrixA, or RangeMatrixB). This includes any other Cells, Range, or related calls that might be missing the worksheet qualifier.
This pattern is explicitly documented: unqualified calls to Excel objects cause Visual Basic to establish its own reference to Excel, which is not released until the program ends and can cause automation to fail on subsequent runs. The documented fix is to always precede calls with the correct object variable, as in:
xlSheet.Range(xlSheet.Cells(1,1), xlSheet.Cells(10,2)).Value = "Hello"
Apply the same approach throughout the matrix routine to restore reliable execution on Windows 11.
References: