The simplified sub below shows line XX1 started to fail yesterday with the above error message. I have tried it on another machine and it fails at the same point.
This code has run hundreds of times, albeit with frequent minor updates. I use this kind of method for selecting a range quite frequently and it usually works fine. It's code that I've written for my own use at work, but aiming to eventually share it with colleagues (for execution and possible ownership). It's part of a 2000-line VBA process.
I've included an earlier fragment of code with a similar line 'xx0' that works fine.
I've found that line XX1 works if I put in an arbitrary selection task beforehand like this: Range("A1").Select
Also, as a test I entered line XX2 and that works fine if I skip over line XX1.
Worse, after experiencing the error, the code continues to run (apparently normally) and then at a later point, it crashes catastrophically: Excel shuts down completely with no warning or message.
I can see that lines xx0 and xx1 are both giving Excel a task to create an address by appending a 'Long' variable to a string like "j2:j" and I guess this is not good practice (it kind of undermines the use of Option Explicit and my Dim statements), but I'm not sure how best to fix it (considering my code contains many subs / 2000 lines and is full of this kind of structure).
Option Explicit
Sub AlignTables()
Dim LastRow As Long
Dim RefColX As Integer
Sheets("shMAIN").Select
LastRow = Cells(999999, 1).End(xlUp).Row
RefColX = Rows("10:10").Find(What:="x1", LookAt:=xlWhole, MatchCase:=False).Column
'Sort shMAIN
Range("c11:c" & LastRow).CurrentRegion.Select
Selection.Sort Header:=xlYes, Key1:=Cells(10, RefColX), Order1:=xlDescending, Key2:=Range("c10"), Order2:=xlAscending
'Refresh index column
Range("c11:c" & LastRow).Select 'Line XX0
Selection.FormulaR1C1 = "=ROW()"
Selection = Selection.Value
'Prepare sort key in ShCleanData column J
Sheets("ShCleanData").Select
LastRow = Cells(999999, 1).End(xlUp).Row 'This sets LastRow to value 6981
Range("j2:j" & LastRow).Select 'Line XX1
Range("j2:j" & "6981").Select 'Line XX2
'(subsequent lines populate the column etc)
End Sub
One option could be to change all my referencing to style YY:
Range(Cells(2,10), Cells(LastRow, 10)).Select 'Style YY
Range("j2:j" & LastRow).Select 'Style XX (easier to follow)
... but I find style XX easier to follow (and easier to explain to people with no VBA knowledge who may need to run and support my code).
KEY QUESTIONS:
- Would declaring LastRow as Variant (instead of Long) be a robust solution to this? [Not only to maintain the 'readable' style, but also to simplify modifying all my subs]
- Or should I go through my code removing Range Selections of type XX and changing them all to style 'YY'?
- Or is there some other better solution?
One other strange observation that may be relevant: I have a VBA Function: jStop ("Run this section stepwise if you're processing data of type XX")
I use this function throughout my code with different messages. The jStop function simply displays the string in a msgbox together with 'Click OK to continue or Cancel to Stop' and handles the user's request to stop (or not). It has been working for many months, but yesterday just before I encountered the 'XX1' issue, I noticed that this function was ignoring the text argument (it behaved like jstop ("")
) ... perhaps this gives a clue about what's behind the XX1 issue? This odd behaviour of jStop did not persist after a reboot.