VBA, Excel keeps crashing

jrwrita

Board Regular
Joined
May 7, 2015
Messages
206
As I am stepping through this section of the code it works but by the time it gets to a certain row it crashes my excel.
Is there a better way to write to my excel sheet? It writes a value cell by cell, not sure why its crashing!

Code:
 For j = 1 To nCols
            If fromTop Then writeVal = arr(i, j) Else writeVal = arr(UBound(arr, 1) - i + 1, j)
            thisWS.Cells(startRow + i - 1, startCol + j - 1).Value = writeVal
        Next j
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
My first thoughts to debugging something like this is to figure out where it keeps crashing.

Add a debug print line like this and keep an eye on your immediate window while stepping to see ~roughly where the program crashes

Code:
 For j = 1 To nCols
            If fromTop Then writeVal = arr(i, j) Else writeVal = arr(UBound(arr, 1) - i + 1, j)
            thisWS.Cells(startRow + i - 1, startCol + j - 1).Value = writeVal
            [COLOR=#ff0000]Debug.Print "i: " & i & " j: " & j[/COLOR]
        Next j

Once you know which line/iteration is crashing, step right up until the line before the problem and then check all the variable values (either with immediate window or hovering mouse over the variables). From there, you can hopefully figure out what should happen on the "problem line".
 
Last edited:
Upvote 0
Thanks alot for this, seems like everytime im stepping through the code the problem line can be on different lines,
one time it iwll be i23 j18 and something else the next time
 
Upvote 0
Do you have the standard optimization code floating around somewhere?

Code:
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = FALSE

I'd also be curious to know if there was a correlation between how fast you step through and how far you are able to progress

I.e. - If you step through really fast, the last "i" value is smaller than if you had stepped slowly.

(This was the case for me a while back and I never figured out the cause)

Also... as an extra check, it's prolly not a bad idea to restart the PC
 
Upvote 0
Its kind of interesting, as I am stepping through each next J im getting quite far...then when I just run the whole thing, crash.
I put your Calc manual and turn off screen updating within the if statement, still crashing.
Any other ideas? Thanks so much for ideas
 
Upvote 0
I've had troubles like this in the past, but the only reasoning behind it was that stepping through slowly gave excel more time to process the operations.

Running, rather than stepping caused a buildup in the process queue and then it would crash. As far as fixing it goes, I'm afraid I'm mostly out of ideas.

How did you assign/build the Arr() array? Did you write a range to it?
 
Upvote 0
The sub is like so, arr() is a variable I assigned as variant ,



Code:
Public Sub myexcel (arr() As Variant, startCell As Range, fromTop As Boolean, nRows As Long, nCols As Long)
   Dim i As Long, j As Long, startRow As Long, startCol As Long
    Dim thisWS As Worksheet
    Dim writeVal As Variant
    
    Set thisWS = startCell.Worksheet
    
    startRow = startCell.Row
    startCol = startCell.column
    
    'clear
    For i = 1 To nRows
        For j = 1 To nCols
            thisWS.Cells(startRow + i - 1, startCol + j - 1).Value = ""
        Next j
    Next i
    
    'write
    For i = 1 To WorksheetFunction.Min(nRows, UBound(arr, 1))
        For j = 1 To nCols
            If fromTop Then writeVal = arr(i, j) Else writeVal = arr(UBound(arr, 1) - i + 1, j)
            thisWS.Cells(startRow + i - 1, startCol + j - 1).Value = writeVal
        Next j
    Next i
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,283
Members
449,075
Latest member
staticfluids

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