insert row in protected sheet by keeping formula

Anonymous
2011-08-13T08:15:07+00:00

Hi

I have a protected sheet contain formula in each row depends to above row( for example C2=C1+1; C3=C2+1;...).and cells contained these formula are locked.

I like to allow users to insert row everywhere by keeping formula without unprotecting the sheet. When I am clicking "insert row" while protecting the sheet, users could insert a row but the locked inserted cells are not including any formula and excel doesn't permit to copy formula of above cells. is there any solution?

Thanks

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
Answer accepted by question author
HansV 462.7K Reputation points MVP Volunteer Moderator
2011-08-14T10:16:32+00:00

If you just want the result of the formula, try

ActiveSheet.Range("E" & r).Value = ActiveSheet.Range("E1").Value

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments
Answer accepted by question author
HansV 462.7K Reputation points MVP Volunteer Moderator
2011-08-13T12:41:40+00:00

In the first place: to prevent the users from viewing the macro and seeing the password to unlock the sheet, you must protect the Visual Basic project of the workbook with a password.

  • In the Visual Basic Editor, select Tools | VBAProject Properties...
  • Activate the Protection tab.
  • Tick the check box "Lock project for viewing".
  • Enter a password in the Password box.
  • Enter the same password in the Confirm password box.
  • Important: don't forget this password!
  • Click OK.
  • Save the workbook.
  • Next time you open the workbook and want to view the Visual Basic code, you'll be prompted for the password.

Let's say the password is "secret". The following macro will insert a row and fill down the formula in column C:

Sub InsertARow()

    Dim r As Long

    On Error GoTo ErrHandler

    ' Get the row number

    r = Range("C10").Value

    ' Unprotect the sheet

    ActiveSheet.Unprotect Password:="secret"

    ' Insert a row

    Range("C" & r).EntireRow.Insert

    ' Fill down the formula from above the inserted row to below it

    Range("C" & (r - 1) & ":C" & (r + 1)).FillDown

ExitHandler:

    ' Protect the sheet again

    ActiveSheet.Protect Password:="secret"

    ' Get out

    Exit Sub

ErrHandler:

    ' Report the error to the user

    MsgBox Err.Description, vbExclamation

    ' Always go past the exit handler section

    Resume ExitHandler

End Sub

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

41 additional answers

Sort by: Newest
  1. Anonymous
    2014-08-02T19:00:42+00:00

    I can see how you did everything in the demo but when I open a spreadsheet to duplicate the VBA code it does not work. Is there a setting or step I am missing or something? Also I tried the tables earlier as a suggestion from a friend it works only when unprotected. So this VBA looks like the viable option I just can't seem to get it to work for some reason.

    I create my table, then create the VBA code, I enabled all macros, and saved my file as macros enabled, the little insert row button never comes up.

    I really appreaciate your help and responsiveness, however, I will be away from the computer for a few hours so you may not hear from me in a little bit if you reply again.

    Thanks

    Was this answer helpful?

    0 comments No comments
  2. HansV 462.7K Reputation points MVP Volunteer Moderator
    2014-08-02T18:03:18+00:00

    I have uploaded a sample workbook to DropBox:

    https://www.dropbox.com/s/wco8l3hg02exii6/InsertDemo.xlsm

    The worksheet has been protected with password secret. The VBA code is not protected.

    The command button lets you insert a row if you select a cell in the data (below the first row with data). Formulas (in this example in column D only, but you could add more) will be copied automatically.

    Another option would be to define a table. Excel will automatically propagate formulas in a table row when you insert a new row.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2014-08-02T17:19:53+00:00

    What i want to be able to do is have a protected sheet with locked and unlocked cells (I know how to do this) I also want to be able to allow my users to insert rows while protected (i am able to make this happen as well). I have certain columns throughout the spreadsheet that have formulas in them and I want to be able to have that formula carry through when they insert a row. Now if it is unprotected I know of a way to get this to happen, what I don't know is when it is locked how to get it to happen.

    Example

    Users enter date in unlocked columns A, B & C through rows 1 through 100, but in column D there is a formula that is based on columns A+B+C.  So say at any point in the spreadsheet on any row while locked and protected I want to be able to allow a user to insert a row, enter data in the unlocked cells and keep the formula in the locked cell continous

    so say D1=SUM(A1+B1+C2), D2=SUM(A2+B2+C2) and so on all the way down to 100 (arbitrary large number that they will like never get to in a month) However sometimes data needs to be grouped together because it is part of the same account so say anywhere in the spreadsheet I want to insert a row anywhere in between the 1 and 100 rows and I want Column D to refelct that formula and result when users enter the date in the newly inserted rows.

    I hope that makes sense. Thanks for you help.

    Was this answer helpful?

    0 comments No comments