Solved

Setting graph label in protected sheet - VBA Excel 2007 SP2

Posted on 2011-03-06
2
448 Views
Last Modified: 2012-05-11
I have a chart in a protected worksheet where a macro sets a data label using the following code
Sub SetLabel()
Dim s As String
With ActiveSheet.ChartObjects(1).Chart
    With .SeriesCollection(3).Points(1)
        s = Range("B2").Value
        .ApplyDataLabels
        .DataLabel.Text = s
    End With
End With
End Sub

Open in new window


The sheet is protected on opening the workbook with UserInterFaceOnly to allow macros to update the protected cells using the code (password replaced by asterisks for this post)

Private Sub Workbook_Open()

    Application.EnableEvents = True
    
    Dim wSheet As Worksheet

    For Each wSheet In Worksheets

        wSheet.Protect Password:="*********", UserInterFaceOnly:=True

    Next wSheet
    
End Sub

Open in new window


With the sheet unprotected the code runs fine.  When it is protected though, the worksheet cells can be accessed by the code but setting the dala label fails on the line "DataLabel.Text = s" with the error message "Method 'Text' of object 'DataLabel' failed".  See attached screen capture for runtime error. Error-when-updating-chart-data-l.docx

The chart has been upprotected as an object by unchecking the Locked option in the Chart tools / Format / Size / Properties tab

Any ideas on how to get past this?  I am releasing the workbook to senior managers and need to make it fairly well tamper proof while retaining the VBA functionality
0
Comment
Question by:sjgrey
2 Comments
 
LVL 14

Accepted Solution

by:
Zack Barresse earned 500 total points
ID: 35053947
Hi,

Just unprotect before you perform your action, then protect afterwards...

Sub SetLabel()
Dim s As String
ActiveSheet.Unprotect Password:="*******"
With ActiveSheet.ChartObjects(1).Chart
    With .SeriesCollection(3).Points(1)
        s = Range("B2").Value
        .ApplyDataLabels
        .DataLabel.Text = s
    End With
End With
ActiveSheet.Protect Password:="*********", UserInterFaceOnly:=True
End Sub

Open in new window


Zack
0
 
LVL 1

Author Closing Comment

by:sjgrey
ID: 35054050
Fantastic thanks
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel Conditional Median Problem 1 15
Auto Populate Day Month  2 digit Date 4 19
Excel VBA 4 27
Manipulate Range in Excel VBA 6 23
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…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

773 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