0

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:

  1. 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]
  2. Or should I go through my code removing Range Selections of type XX and changing them all to style 'YY'?
  3. 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.

LondonJustin
  • 73
  • 1
  • 1
  • 7
  • 3
    Read [How to avoid select](https://stackoverflow.com/q/10714251/16578424) and update your code accordingly. Most likely the problem is solved then. – Ike Sep 28 '22 at 09:38
  • Thank you Ike. That thread contains great advice that I can incorporate, and I’ve been dreaming of finding a thread like that for years. I'm sure I will use those techniques. But for this situation, it's important to be able to watch the code running stepwise (because the task changes a lot ... exceptions etc) and so for now, I will target the lines that seem to be treating declared variables as variants and change their style from 'XX' to 'YY'. There are fewer than I had feared. – LondonJustin Sep 28 '22 at 12:17
  • I have changed all references to style YY and the macro seems to be working OK. Any more ideas would be valued, especially any other possible solutions other than reduced use of 'selects'. It enabled me to look at the 'catastrophic crash' I described, and that was happening when I tried to update a cell comment. (I resolved that last problem by removing the cell cmt functionality from my code and removing comments from the sheet. I assume this is unrelated to the original issue.) – LondonJustin Sep 28 '22 at 15:25
  • 1
    There's no problem with Style XX, and `LastRow` should be a Long - no reason to switch it to Variant – Tim Williams Sep 28 '22 at 17:56
  • Thank you @Tim for your reassurance on both of those points. The fact it also failed on a helpdesk 'loaner' PC at the same place (after already successfully executing another Style XX line earlier in the sub) forced me to change _something_. My instinct was against changing the type of `LastRow` and so I opted to switch to style YY and make a mental note that Excel may occasionally object to things like `Range(strVariable & longVariable).Select` . – LondonJustin Sep 29 '22 at 08:21

0 Answers0