Chart and Shape object protection?

rsjury

New Member
Joined
Jun 28, 2014
Messages
48
Trying to figure out with VBA how to protect and unprotect specific objects in one sheet. In other words, I have one sheet with many Charts and Shapes, I want to choose which ones are protected and which ones are not. I figure that I can use the "name" for each object I just don't know the VBA Syntax. if someone can just give me a couple samples I could figure it out. The samples I would like are (unlock all charts but lock all shapes and choosing which specific Charts and Shapes to protect or not). THANKS IN ADVANCE!
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I guess the sheet is protected.
Change "abc" for the password of your sheet.

With the following, unlock all shapes and charts and lock specific shapes and charts.

VBA Code:
Sub Lock_Shape_Chart()
  Dim sc As Shape
  
  Application.ScreenUpdating = False
  With ActiveSheet
    .Unprotect "abc"
    .Shapes.SelectAll
    Selection.Locked = False  'Unlock all Shapes and Charts
    '
    For Each sc In .Shapes
      Select Case sc.Name
        'Lock specific Shapes and Charts
        Case "Shape1", "Shape3", "Shape5", "etc", _
             "Chart2", "Chart4", "Chart6", "etc"
          sc.Locked = True
      End Select
    Next
    Range("A1").Select
    .Protect "abc"
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
I guess the sheet is protected.
Change "abc" for the password of your sheet.

With the following, unlock all shapes and charts and lock specific shapes and charts.

VBA Code:
Sub Lock_Shape_Chart()
  Dim sc As Shape
 
  Application.ScreenUpdating = False
  With ActiveSheet
    .Unprotect "abc"
    .Shapes.SelectAll
    Selection.Locked = False  'Unlock all Shapes and Charts
    '
    For Each sc In .Shapes
      Select Case sc.Name
        'Lock specific Shapes and Charts
        Case "Shape1", "Shape3", "Shape5", "etc", _
             "Chart2", "Chart4", "Chart6", "etc"
          sc.Locked = True
      End Select
    Next
    Range("A1").Select
    .Protect "abc"
  End With
  Application.ScreenUpdating = True
End Sub
Thanks so much, will be trying this out later today!!
 
Upvote 0
I guess the sheet is protected.
Change "abc" for the password of your sheet.

With the following, unlock all shapes and charts and lock specific shapes and charts.

VBA Code:
Sub Lock_Shape_Chart()
  Dim sc As Shape
 
  Application.ScreenUpdating = False
  With ActiveSheet
    .Unprotect "abc"
    .Shapes.SelectAll
    Selection.Locked = False  'Unlock all Shapes and Charts
    '
    For Each sc In .Shapes
      Select Case sc.Name
        'Lock specific Shapes and Charts
        Case "Shape1", "Shape3", "Shape5", "etc", _
             "Chart2", "Chart4", "Chart6", "etc"
          sc.Locked = True
      End Select
    Next
    Range("A1").Select
    .Protect "abc"
  End With
  Application.ScreenUpdating = True
End Sub
This is what I ended up with! Thanks for your help it works perfect!

VBA Code:
Sub ProtectTool()
    Dim SC As Shape
    
    Application.ScreenUpdating = False
    On Error Resume Next
    
    For Each ws In Worksheets
    With ws

        If Not ws.CodeName Like "Lock*" Then ws.Protect _
        DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFiltering:=True, password:=AdminPW
        
        If ws.CodeName Like "Lock*" Then ws.Protect _
        Contents:=True, Scenarios:=True, AllowFiltering:=True, password:=pw
        For Each SC In .Shapes
        Select Case SC.Name
        Case "TotalChart", "IndianaChart", "CaliforniaChart"
        SC.Locked = False
        Case "Unprotect", "Protect" 'Buttons
        SC.Locked = True
        End Select
        Next SC
    End With
    Next ws
       
    ThisWorkbook.Protect password:=AdminPW, Structure:=True, Windows:=False
    
    On Error GoTo 0
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,666
Messages
6,173,665
Members
452,527
Latest member
ineedexcelhelptoday

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top