This VBA code crashes my Excel

6diegodiego9

Board Regular
Joined
Jan 9, 2018
Messages
80
Office Version
  1. 2016
Platform
  1. Windows
Just write this code in a new module and launch a debug - compile:

VBA Code:
Option Explicit

    Private Type seq
        Count As Long
        'rng As Range
    End Type
Private Type xlTable
    seq As seq
    rng As Range
End Type

Sub REALE()
    Dim dbl As Double: dbl = Sheet1.Range("ABC").Value2
   
    Dim xlTable As xlTable
    Set xlTable.seq.rng = xlTable.rng.Offset(0, 4)
End Sub

It crashes my Excel on two different PCs.

I know that there are atleast three errors in this code: the named range "ABC" doesn't exist, xlTable.seq.rng doesn't exist and xlTable.rng is not defined.

Anyway, what's the cause of the crash?
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
You have a variable declaration and an assignment of that same variable as a one-liner at the start of your procedure. The non-existence of the rng variable results in a compile error. If an attempt is made to run the code despite that, the run-time error and the compile-time error fight for precedence. Neither wins, Excel crashes.
 
Upvote 0
xlTable.seq.rng
In the above, rng is not a member of seq. You probably wanted to do xlTable.rng instead, but as @GWteB has already mentioned, you can't assign it a non-existing variable.
 
Upvote 0
Wait wait, I never tried to "run the code", I just try to "debug - compile" it to spot the errors and on the (second) compile Excel crashes.

The first Compile correctly highlights an error:

1638956899308.png



while the second consecutive Compile instead than highlighting the (same) error, just stay hang for some second with this screen ("Compiling...") then Excel crashes:

1638956846679.png


Is it a compiler bug? what triggers it?
 
Upvote 0
I am running Excel 2010 and I get exactly the same results as you, excel crashes on the second compile!!
 
Upvote 0
@6diegodiego9 & @offthelip you're both right. I've looked into it a little further out of interest and have to revise my previous comment in part.

The crash is not caused by a (possible) run-time error and a compile error at the same time. It's just a (or another) bug in the compiler.
I don't pretend to be exhaustive, but Excel crashes anyway when the conditions mentioned below are met:
  1. Option Explicit is used - AND
  2. the very first VBA method/function within the procedure is on the same line of code as which a variable is declared - AND
  3. an attempt is made to assign a Range object to an undeclared variable using a pre-declared variable (vartype Range) using one of the following properties: Cells, Cells(), Offset(), Range(), Resize().
Translated to VBA code:
VBA Code:
Option Explicit                     ' #1

Public SomeRange As Range           ' #3.1

Sub ExcelCrash()
    Dim a
    Dim b: MsgBox "Bye"             ' #2
    '
    '
    Set r = SomeRange.Cells(1, 1)   ' #3.2
    '
End Sub
 
Upvote 0
I tried deleting the option explicit but Excel still crashed with my 2010. However I agree this is another bug in the compiler and it obviously it isn't much of a problem because the code also obviously wrong. Compiler bugs are much more of problem when they are apparent on legal code.
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top