[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I avoid run-time error when a worksheet is protected

Posted on 2014-03-19
6
Medium Priority
?
765 Views
Last Modified: 2014-03-19
Greetings.

I'm using the following code to update a cell comment. If I protect the worksheet then I receive a runtime error? How can I avoid this, but still have the worksheet protected?

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

   Dim Cell As Range

   If Intersect([Q12:Q61], Target) Is Nothing Then
      On Error Resume Next
      For Each Cell In [Q12:Q61]
           Cell.Comment.Delete
           Cell.Value = ""
      Next Cell
   Else
      For Each Cell In Intersect([Q12:Q61], Target)
         If Len(Me.Cells(Cell.Row, "R").Value) > 0 Then
            On Error Resume Next
            Cell.Comment.Delete
            On Error GoTo 0
            Cell.Value = "Click here to check your quality rigour"
            Cell.AddComment Me.Cells(Cell.Row, "R").Value
            Cell.Comment.Shape.ScaleWidth 2.26, msoFalse, msoScaleFromTopLeft
            Cell.Comment.Shape.ScaleHeight 2.26, msoFalse, msoScaleFromTopLeft
          End If
      Next Cell
   End If

End Sub

Open in new window

0
Comment
Question by:zerog
6 Comments
 
LVL 85

Assisted Solution

by:Rory Archibald
Rory Archibald earned 200 total points
ID: 39939323
You could try protecting the sheet using the UserInterfaceOnly:=True option when you open the workbook (the setting doesn't persist when closing and reopening):
Private Sub Workbook_Open()
ThisWorkbook.Sheets("sheetname").Protect Password:="whatever", UserInterfaceOnly:=True
End Sub

Open in new window

0
 

Author Comment

by:zerog
ID: 39939372
I put the lines of code you provided in "ThisWorkbook" or should it be in the code for that Sheet.

I still get the runtime error. When I debug it highlights the following in my code

Cell.AddComment Me.Cells(Cell.Row, "R").Value

Open in new window

0
 
LVL 34

Assisted Solution

by:Rob Henson
Rob Henson earned 200 total points
ID: 39939375
When protecting the sheet, there are a list of boxes for what users are able to do within the protection. Scroll down and tick the Edit Objects box. Users will then be able to edit comments.

Thanks
Rob H
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 85

Expert Comment

by:Rory Archibald
ID: 39939389
Did you actually run the code?
0
 
LVL 4

Accepted Solution

by:
senthilkumarsb earned 1600 total points
ID: 39939398
I have taken your  code as example. Unprotect the worksheet before you do anything after doing your add/update/delete on the worksheet protect the worksheet again. I hope this will help you.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
   Dim Cell As Range
ThisWorkbook.Sheets("sheetname").UnProtect Password:="xxxxx"
   If Intersect([Q12:Q61], Target) Is Nothing Then
   ...
   ...
   ...
   ...
   ...
   End If
ThisWorkbook.Sheets("sheetname").Protect Password:="xxxxx"
End Sub
0
 

Author Closing Comment

by:zerog
ID: 39939443
Rory Archibald: I inserted the code in "ThisWorkbook" and it runs when opening the workbook

robhenson: Selecting the "Edit Objects" box did solve the error

I chose to go with the solution from senthilkumarsb as it integrated well with the existing piece of code I was using.

Thank you to all for the quick responses. Much appreciated
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question