Solved

Setting graph label in protected sheet - VBA Excel 2007 SP2

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

752 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