You are here:

Excel/VBA and protected worksheet


Hi Tom

I have a some code that is in a particular worksheet.  It works perfectly well.  However when the worksheet is password protected the code does not run.  Is there any way around this?

Thanks in advance

Chris Mitchell

Private Sub CommandButton1_Click()
If Range("A1").Value = 1 Then
 CommandButton1.Caption = "Turn Tables ON"
 Me.Range("A1").Value = 1
 CommandButton1.Caption = "Turn Tables OFF"
End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim r As Range, r1 As Range, shp As Shape
Dim ans As Long
Set r = Range("I2:L7")
' area where this event will be active
Set r1 = Range("B12:AY61")
Set r1 = r1.Resize(r1.Rows.Count - r.Rows.Count + 1, _
 r1.Columns.Count - r.Columns.Count + 1)
If Target.Count = 1 Then
If Range("A1").Value = 1 Then
 If Not Intersect(Target, r1) Is Nothing Then
    Set shp = ActiveSheet.Shapes.AddShape( _
     Type:=msoShapeRectangle, _
     Left:=Target.Left, _
     Top:=Target.Top, _
     Width:=r.Width, _
    Application.ScreenUpdating = False
    Application.ScreenUpdating = True
    ans = MsgBox("          OK?", vbYesNo)
    Application.ScreenUpdating = False
    Application.ScreenUpdating = True
    If ans = vbNo Then
      Exit Sub
    End If
   On Error Resume Next
    r.Copy Target
    If Err.Number <> 0 Then
      MsgBox "Bad Location, try again"
    End If
   On Error GoTo 0
 End If
End If
End If
End Sub

Christopher Mitchell,

You need to unlock the cells in the box area and in cell A1.  with the sheet unprotected, you select those cells and right click - and select format cells - then go to the protection tab and uncheck "Locked"

then get out.

Now protect the worksheet, but check  

Select locked and unlocked cells, format cells, and Edit objects.

with this combination it worked for me.  I did very light testing, so you can experiment with settings and see what works for you.

Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Tom Ogilvy


Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]


Extensive experience.

Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 All rights reserved.