Excel crashes when inserting (and occasionally when deleting) rows in VBA. I have tried the following fixes, but none helped:
I've used various methods to insert rows. All crash Excel:
The error message (when it appears; most times it doesn't) is an EXCEL.EXE Application Error:
"The instruction at __________referenced memory at ___________. The memory could not be read.
Here's the module in its entirety:
- Ran the Microsoft Office Repair program for Office 2010
- Deactivated all ADD-IN modules
- Tried: Application.EnableEvents = False
I've used various methods to insert rows. All crash Excel:
- ActiveCell.EntireRow.Insert Shift:=xlShiftDown
- Dim SelectedRange As Range
Set SelectedRange = Selection
SelectedRange.EntireRow.Insert - Dim SelectedRange As Range
Set SelectedRange = Selection.EntireRow
SelectedRange.Insert
The error message (when it appears; most times it doesn't) is an EXCEL.EXE Application Error:
"The instruction at __________referenced memory at ___________. The memory could not be read.
Here's the module in its entirety:
Code:
Option Explicit
Sub Insert_Row()
'
' Inserts a row above the selected row
' Copies the selected plant/size from the ComboBox linked cell to the new inserted row in column 3 (offset 2)
'
On Error GoTo Error_handler:
Dim Response As Integer
Dim SelectedRange As Range
Set SelectedRange = Selection.EntireRow 'Save entire row selection based on active cell
' Ensure plant has been selected from dropdown list
If Range("G1").Value = "" Then 'Test LinkedCell to see if plant selection has been made
MsgBox "Please select a plant from the dropdown list"
Exit Sub
End If
' Unprotect sheet & Hide screen updates
ActiveSheet.Unprotect
Application.ScreenUpdating = False
Application.EnableEvents = False
' Insert row above selection and paste contents of selected row to inserted row
' SelectedRange.EntireRow.Insert 'CREATES FATAL ERROR
' ActiveCell.EntireRow.Insert 'TRIED THIS --> NOPE: This crashed too
' (FROM https://www.mrexcel.com/forum/excel-questions/77371-vba-insert-row-colum.html)
' ActiveCell.EntireRow.Insert Shift:=xlShiftDown 'TRIED THIS --> NOPE: This crashed too
' (FROM https://powerspreadsheets.com/excel-vba-insert-row/#Specify-the-Active-Cell-with-the-ApplicationActiveCell-Property)
'
' ONLY WAY TO INSERT A ROW WITHOUT CRASHING: Put this Msgbox code in front of the INSERT statement
Response = MsgBox("Is Entire Row Selected?", vbOKCancel, "Delete Row")
If Response = vbCancel Then End
If Response = vbYes Then
End If
SelectedRange.Insert Shift:=xlShiftDown
SelectedRange.RowHeight = 14.25 'Reset row height (narrow row after last might be selected)
SelectedRange.ClearContents 'Clear contents of inserted row
' Copy selected plant from dropdown list and paste into plant name column of inserted row
Range("G1").Copy
Range(Cells(Selection.Row, 3).Address).PasteSpecial xlPasteAll 'Paste plant name into Column 3
' Copy TOT COST formula from row below and paste into TOT COST column of inserted row
Range(Cells(Selection.Row + 1, 5).Address).Copy 'Copy TOT COST formula from row below
Range(Cells(Selection.Row, 5).Address).PasteSpecial xlPasteAll 'Paste TOT COST formula into Column 5
GoTo Endinsert
Error_handler:
MsgBox Err.Description
Endinsert:
' Protect sheet & Show screen updates
ActiveSheet.Protect
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub