Solved

Setting graph label in protected sheet - VBA Excel 2007 SP2

Posted on 2011-03-06
2
445 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

757 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

16 Experts available now in Live!

Get 1:1 Help Now