Solved

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

Posted on 2014-03-19
6
632 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 32

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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from 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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

813 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

11 Experts available now in Live!

Get 1:1 Help Now