Insert row anywhere in the range and copy formula from above on protected sheet

Akira181

Board Regular
Joined
Mar 23, 2010
Messages
67
Office Version
  1. 365
Platform
  1. Windows
I've tried searching but every solution I've found so far either inserts the new row at the bottom of the table or is done via a click button and my VBA skills aren't capable of modifying the code. I'd like it to function without additional user input, just right click, insert new row, and done ideally.

I have a table with data in B8:Q8, up to 50ish rows. Columns B to K + Q are unprotected for user input. Columns L to O have formulas and those cells are protected. Every undefined number of rows, there's also a subtotal row so L to O have a different formula. Currently L to O are protected on this row too but can be changed if necessary.

What I'd like to happen is if the user inserts a row anywhere on the sheet, I would like the formulas in Cells L to O to be copied in (the data formulas, not the subtotal formula). Logically thinking, the user will most likely insert a new row above the subtotal row, so I'd need the formula from above to be copied down into the new row.

Can someone help with this?
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Some more googling and modifying it to suit my needs, I got this to almost work:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  ActiveSheet.Unprotect ""
  Set rng = Intersect(Target.EntireRow, Range("B:Q"))
  If Application.Count(rng) > 0 Then Exit Sub
  rng.Offset(-1).Copy
  rng.Select
  On Error GoTo enableexit
  ActiveSheet.Paste
  Application.CutCopyMode = False
  rng.SpecialCells(xlConstants).ClearContents
  ActiveSheet.Protect "", AllowInsertingRows:=True, AllowDeletingRows:=True

enableexit:
Application.EnableEvents = True
End Sub

Only problem is AllowDeletingRows part doesn't work. I'm getting "the row you're trying to delete contains a locked cell"
 
Upvote 0

Forum statistics

Threads
1,215,467
Messages
6,124,984
Members
449,201
Latest member
Lunzwe73

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