Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Setting graph label in protected sheet - VBA Excel 2007 SP2

Posted on 2011-03-06
2
Medium Priority
?
479 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 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
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 the scrolling table in Microsoft Excel using the INDEX function.

879 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