Solved

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

Posted on 2014-03-19
6
613 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 50 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 31

Assisted Solution

by:Rob Henson
Rob Henson earned 50 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 85

Expert Comment

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

Accepted Solution

by:
senthilkumarsb earned 400 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now