VBA code to insert multiple non consecutive rows based on selected rows within a table

rjheibel

New Member
Joined
Mar 8, 2018
Messages
42
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,

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
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: VBA code to insert multiple non consecutive rows based on selected rows within a table
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,857
Members
449,051
Latest member
excelquestion515

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