Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-03-19
6
Medium Priority
?
750 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 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 33

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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.
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 in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

618 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