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. HansV 462.7K Reputation points MVP Volunteer Moderator
    2014-08-02T15:53:00+00:00

    The requirement in this thread was that the user would enter a row number in cell C10, and that the macro would insert a row at that row number. So if the user enters 37 in C10, a row would be inserted at row 37.

    You probably want something else - can you explain what exactly?

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2014-08-02T14:17:55+00:00

    Hello Hans V

    I have a problem similar to this and I tried testing your formula and I could not get it to work. I have Office 2007.  Basically I have a spreadsheet I want to keep protected and I have formulas in it, I lock the cells with formulas, but allow for my users to put their data in unlocked cells. However on occasion they may need to insert a row, I know how to give them that capability the formula just doesn't follow when protected.

    I made an excel spreadsheet just so I could test the formula above where Column C would be where the formula needs to follow through and I can't get it to work. On some other formula's I have seen the range is not given as C10 but C1:C100 or whatever is needed. I don't know if this has an affect. I am fairly ignorant of some of this stuff, but I have certainly gotten a crash course in how to do a whole bunch of excel stuff I never thought i would do, need or excel was capable of doing.

    I know this thread is a little old but I am hoping you are still available to give me an answer.

    Thanks

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  3. Anonymous
    2011-10-21T07:24:43+00:00

    Sir,

    Namaskar! I have a sheet having column A to T and row number is 677. Most of the cells have formula.

    Can i put a row containing formula of above cells only when i make entry in last row. For example if last row is 677 and i make entry in this row. as soon as 677 entry passed nex row 678 should insert automatically.

    Regards

    RR Singh

    Was this answer helpful?

    0 comments No comments