Unprotect sheet, copy formula and protect sheet again

delphy

New Member
Joined
Feb 27, 2007
Messages
8
I'm working in Excel 2000 (I know -- first mistake) on a spreadsheet that I wanted protected (for the formulas), but the user has to be able to add a row and copy the formula that is in the row above, down into the row they just inserted.

The sheet consists of six columns, with different formulas in each. If you were just working on the sheet, you would select the bottom cells of the row and use the handle to drag the formulas down one row. The code can't refer to static cell labels because rows will be inserted and deleted from the sheet.

So I think I need a macro that will: (1) unprotect the password-protected sheet; (2) insert a row above the current insertion point (3) copy the formulas down from the row above and then (4) password protect the sheet again.

Doesn't seem that tough, but I don't work with this stuff enough to keep sharp. Thanks.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Assuming the columns with the formulas you want to copy are A:F. If not, adjust the range below. First, highlight the row beneath the one where you want to insert a new row, and run this macro:

Code:
Sub Macro2()
    ActiveSheet.Unprotect Password:="your_password"
    Selection.Insert Shift:=xlDown
    ActiveCell.Offset(-1, 0).Range("A1:F1").Select
    Selection.Copy
    ActiveCell.Offset(1, 0).Range("A1").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveSheet.Protect Password:="your_password", DrawingObjects:=True, Contents:=True, Scenarios:=True
    Range("A1").Select
End Sub
 
Upvote 0
This is exciting because that's the closest I've ever gotten! The macro worked in that it added a row and copied the formulas down from the row above. However, it also moved the contents of columns b through f, from the row below the insertion point, up one row. Everthing that was in B29:F29 is now in B28:F28.
 
Upvote 0
Below is a before and after sample of the ranges the macro affects. In my sample, I selected row 3 and ran the macro. It inserted a new row above row 3 and copied the formulas in A2:F2 down to the new A3:F3.

If it's not working for you, check the ranges in the macro to the ranges you are trying to copy.
Book1
ABCDEFG
1Before
22222222
33333333
44444444
55555555
66666666
Sheet1
Book1
ABCDEFG
1After
22222222
3000000
43333333
54444444
65555555
76666666
Sheet1
 
Upvote 0
Okay, here's the difference. If possible, I don't want the user to have to select the row before they run the macro. If possible, I just want them to be able to click within the row - without selecting it - and run the macro to insert a row above and copy the formulas. It's more idiot proof that way. Thank you so much for your help!
 
Upvote 0
O.K., try this. You can be in any cell in the row and insert a row above it.

Code:
Sub Macro3()
    ActiveSheet.Unprotect Password:="your_password"
    ActiveCell.Rows("1:1").EntireRow.Select
    Selection.Insert Shift:=xlDown
    ActiveCell.Offset(-1, 0).Range("A1:F1").Select
    Selection.Copy
    ActiveCell.Offset(1, 0).Range("A1").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveSheet.Protect Password:="your_password", DrawingObjects:=True, Contents:=True, Scenarios:=True
    Range("A1").Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,974
Messages
6,122,536
Members
449,088
Latest member
RandomExceller01

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