Hi, I currently have the code below that will insert rows into a protected sheet base on the row/rows selected by the user. I added a command to the Context Menu to run this macro when the user right clicks on a row number. It works very well except when the user selects multiple nonconsecutive rows, I get a " Run-time error '1004': Insert method of Range class Failed". I believe this is due to the fact that excel does not let you insert multiple rows when within a Table. Although I would love for the user to be able to add these rows as selected, the biggest problem I have with this is that the sheet is left unprotected after the error is received.
Is there any way to modify this code to allow the user to insert multiple non-consecutive rows when a table is used? If not, how would I modify the code to either give the user a msgbox stating that they cannot add the rows as selected, or at a minimum how would I add code to protect the sheet prior to erroring out?
Thanks,
Is there any way to modify this code to allow the user to insert multiple non-consecutive rows when a table is used? If not, how would I modify the code to either give the user a msgbox stating that they cannot add the rows as selected, or at a minimum how would I add code to protect the sheet prior to erroring out?
Thanks,
VBA Code:
Public Sub insertProtectedRow()
Application.EnableCancelKey = xlDisabled
Dim ws As Worksheet
On Error Resume Next
Set ws = ActiveWorkbook.Sheets("Schedule")
On Error GoTo 0
If ws Is Nothing Then
MsgBox "Function Only Works in Schedule Sheet!!!"
Else
If ws.Name = ActiveWorkbook.ActiveSheet.Name Then
If ActiveCell.row > 5 Then
ActiveSheet.Unprotect "password"
Selection.EntireRow.Insert
ActiveSheet.Protect _
Password:="password", _
DrawingObjects:=True, _
Contents:=True, _
Scenarios:=True, _
AllowFormattingCells:=True, _
AllowFiltering:=True
Else
MsgBox "Can Not Insert Above Row #5, Please Try Again."
End If
Else
MsgBox "Function Only Works in Schedule Sheet!!!"
End If
End If
Application.EnableCancelKey = xlInterrupt
End Sub