Solved

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

Posted on 2014-03-19
6
705 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 33

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
Independent Software Vendors: 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 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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

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…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

690 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