Excel crashes after running specific macro

LauraJane

New Member
Joined
Feb 9, 2015
Messages
7
Hi!

Every time I run the following macro, Excel crashes. I've tried it on two different machines one with 365, and the other with Microsoft Office Professional Plus 2016. I've turned off add-ins, and copied and pasted the code from the workbook module to the personal file. The file I'm running the macro on is an xlsx file. When i step through the code, the macro works fine and does not crash. The data that the macro is working on has a count of 5,915,363 cells, and the data is in a table.

Code:
Sub ConvertFormulas()'
' ConvertFormulas Macro
'


Dim rng As Range, cell As Range
Set rng = Range("STARData")
    rng.Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    For Each cell In rng
        If cell.Value = "x" Then
            cell.ClearContents
        End If
    Next cell
End Sub

I don't know what to try next.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Not sure why your code would make it crash. Try the following code and see if it fixes the crashing issue. Either way, the code below should be faster since it's much quicker to work with data in an array than to be constantly reading from and writing to the worksheet.

Code:
Sub fxToValue()
Application.ScreenUpdating = False
Dim r As Range:         Set r = Range("STARTData")
Dim AR() As Variant:    AR = r.value


For i = LBound(AR) To UBound(AR)
    For j = LBound(AR, 2) To UBound(AR, 2)
        If AR(i, j) = "x" Then AR(i, j) = vbNullString
    Next j
Next i


r.value = AR
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Maybe another option...

Code:
Sub STARDataClr()
    Application.ScreenUpdating = False
    With Range("STARData")
        .Value = .Value
        .Replace "x", "#N/A", xlWhole, , True, False, False
        .SpecialCells(xlConstants, xlErrors).ClearContents
    End With
    Application.ScreenUpdating = True
End Sub


Not sure how it will perform with a range of 5,915,363 cells :eeek: though.
 
Last edited:
Upvote 0
Irrobo314 - Your code worked beautifully - it ran in a matter of seconds. Thank you!
 
Last edited:
Upvote 0
───────────────────░█▓▓▓█░▇▆▅▄▃▂
──────────────────░█▓▓▓▓▓█░▇▆▅▄▃▂
─────────────────░█▓▓▓▓▓█░▇▆▅▄▃▂
──────────░░░───░█▓▓▓▓▓▓█░▇▆▅▄▃▂ ...
─────────░███░──░█▓▓▓▓▓█░▇▆▅▄▃▂
───────░██░░░██░█▓▓▓▓▓█░▇▆▅▄▃▂
──────░█░░█░░░░██▓▓▓▓▓█░▇▆▅▄▃▂
────░██░░█░░░░░░█▓▓▓▓█░▇▆▅▄▃▂
───░█░░░█░░░░░░░██▓▓▓█░▇▆▅▄▃▂
──░█░░░░█░░░░░░░░█▓▓▓█░▇▆▅▄▃▂
──░█░░░░░█░░░░░░░░█▓▓▓█░▇▆▅▄▃▂
──░█░░█░░░█░░░░░░░░█▓▓█░▇▆▅▄▃▂
─░█░░░█░░░░██░░░░░░█▓▓█░▇▆▅▄▃▂
─░█░░░░█░░░░░██░░░█▓▓▓█░▇▆▅▄▃▂
─░█░█░░░█░░░░░░███▓▓▓▓█░▇▆▅▄▃▂
░█░░░█░░░██░░░░░█▓▓▓▓▓█░▇▆▅▄▃▂
░█░░░░█░░░░█████▓▓▓▓▓█░▇▆▅▄▃▂
░█░░░░░█░░░░░░░█▓▓▓▓▓█░▇▆▅▄▃▂
░█░█░░░░██░░░░█▓▓▓▓▓█░▇▆▅▄▃▂
─░█░█░░░░░████▓▓▓▓██░▇▆▅▄▃▂
─░█░░█░░░░░░░█▓▓██▓█░▇▆▅▄▃▂
──░█░░██░░░██▓▓█▓▓▓█░▇▆▅▄▃▂
───░██░░███▓▓██▓█▓▓█░▇▆▅▄▃▂
────░██▓▓▓███▓▓▓█▓▓▓█░▇▆▅▄▃▂
──────░█▓▓▓▓▓▓▓▓█▓▓▓█░▇▆▅▄▃▂
──────░█▓▓▓▓▓▓▓▓▓▓▓▓▓█░▇▆▅▄▃▂
 
Upvote 0
Not sure how it will perform with a range of 5,915,363 cells :eeek: though.

Not particularly well. I tested it and it took a long while, ~10 mins on some test data. For a minute there I thought that they'd have to use yours because writing that much data from an array can sometimes result in errors, but I think that only that happens when your're using Application.Transpose(array). I was surprised to see that there was such a discrepancy. I wonder why the replace takes so much longer...
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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