68

I am using Excel where certain fields are allowed for user input and other cells are to be protected. I have used Tools Protect sheet, however after doing this I am not able to change the values in the VBA script. I need to restrict the sheet to stop user input, at the same time allow the VBA code to change the cell values based on certain computations.

1

6 Answers 6

101

Try using

Worksheet.Protect "Password", UserInterfaceOnly := True

If the UserInterfaceOnly parameter is set to true, VBA code can modify protected cells.

Note however that this parameter does not stick. It needs to be reapplied each time the file is opened.

6
  • 2
    This is a better solution, of course. If you come back and see this comment, though, please fix the typo in your code - there's an "f" instead of a "t": UserInferfaceOnly It took a couple minutes to figure out what was wrong after I c&p'ed your code.
    – Kevin
    Sep 24, 2008 at 16:40
  • This unfortunately seems to fail, see here Feb 9, 2015 at 11:52
  • What if I only want to edit a specific range of cells?
    – user2933315
    Aug 1, 2015 at 19:52
  • 1
    @Solver - if you want to allow the user to edit cells on a protected worksheet, you need to unlock those cells (Range.Locked = False in VBA).
    – Joe
    Aug 2, 2015 at 10:52
  • 2
    UserInterfaceOnly would be beautiful if MS maintained it for new features. If I try refreshing a query table through VBA on a protected sheet with UserInterfaceOnly = true, it fails, I have to unprotect the sheet, refresh, then protect again. Arghhhh....
    – Travis
    Jul 12, 2018 at 22:32
21

You can modify a sheet via code by taking these actions

  • Unprotect
  • Modify
  • Protect

In code this would be:

Sub UnProtect_Modify_Protect()

  ThisWorkbook.Worksheets("Sheet1").Unprotect Password:="Password"
'Unprotect

  ThisWorkbook.ActiveSheet.Range("A1").FormulaR1C1 = "Changed"
'Modify

  ThisWorkbook.Worksheets("Sheet1").Protect Password:="Password"
'Protect

End Sub

The weakness of this method is that if the code is interrupted and error handling does not capture it, the worksheet could be left in an unprotected state.

The code could be improved by taking these actions

  • Re-protect
  • Modify

The code to do this would be:

Sub Re-Protect_Modify()

ThisWorkbook.Worksheets("Sheet1").Protect Password:="Password", _
 UserInterfaceOnly:=True
'Protect, even if already protected

  ThisWorkbook.ActiveSheet.Range("A1").FormulaR1C1 = "Changed"
'Modify

End Sub

This code renews the protection on the worksheet, but with the ‘UserInterfaceOnly’ set to true. This allows VBA code to modify the worksheet, while keeping the worksheet protected from user input via the UI, even if execution is interrupted.

This setting is lost when the workbook is closed and re-opened. The worksheet protection is still maintained.

So the 'Re-protection' code needs to be included at the start of any procedure that attempts to modify the worksheet or can just be run once when the workbook is opened.

3

A basic but simple to understand answer:

Sub Example()
    ActiveSheet.Unprotect
    Program logic...
    ActiveSheet.Protect
End Sub
1
  • This is what i simply needed. Straightforward answer to my requirement.Thanks Jun 1, 2021 at 9:04
2

I don't think you can set any part of the sheet to be editable only by VBA, but you can do something that has basically the same effect -- you can unprotect the worksheet in VBA before you need to make changes:

wksht.Unprotect()

and re-protect it after you're done:

wksht.Protect()

Edit: Looks like this workaround may have solved Dheer's immediate problem, but for anyone who comes across this question/answer later, I was wrong about the first part of my answer, as Joe points out below. You can protect a sheet to be editable by VBA-only, but it appears the "UserInterfaceOnly" option can only be set when calling "Worksheet.Protect" in code.

1
  • This is only true for Excel 95 and 97. For Excel 2000+, see the Joe's answer.
    – GSerg
    Sep 24, 2008 at 10:32
2

As a workaround, you can create a hidden worksheet, which would hold the changed value. The cell on the visible, protected worksheet should display the value from the hidden worksheet using a simple formula.

You will be able to change the displayed value through the hidden worksheet, while your users won't be able to edit it.

0

I selected the cells I wanted locked out in sheet1 and place the suggested code in the open_workbook() function and worked like a charm.

ThisWorkbook.Worksheets("Sheet1").Protect Password:="Password", _
UserInterfaceOnly:=True

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.