Solved

VBA to change formula in data label

Posted on 2013-12-17
3
1,613 Views
Last Modified: 2016-06-14
Hello,

I used Rob Bovey's xy chart labeler to label a dataseries in a scatterplot. Each datalabel refers to a cell in the worksheet, such as "=BE_Cat!$D$30", so the data label contains whatever is in cell d30, d31 etc.

Unfortunately, I (or Excel) screwed up and have many, many files and charts where the label formula refers to the wrong sheet, e.g. "=CV_Cat!$D$30" instead of "=BE_Cat!$D$30".

To fix this, I'm looking for a macro to select a certain dataseries in a chart (Let's call it "CV"), and then circle through all the data labels in the series to change "CV" to "BE" in the label formula, i.e. from  "=CV_Cat!$D$30" to "=BE_Cat!$D$30", "=CV_Cat!$D$31" to "=BE_Cat!$D$31", and so on.

Thank you!!!!
0
Comment
Question by:ejortberg
[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
3 Comments
 
LVL 17

Accepted Solution

by:
andrewssd3 earned 400 total points
ID: 39725908
This will work for the active chart (i.e. the clicked-on selected chart):
Public Sub ReformatLabels()

    Dim c As Chart
    Dim s As Series
    Dim p As Point
    
    Const FROM_STR As String = "CV"
    Const TO_STR As String = "BE"
    
    Set c = ActiveChart

    Set s = Nothing
    On Error Resume Next
    Set s = c.SeriesCollection("CV")
    On Error GoTo 0
    
    If Not s Is Nothing Then
    
        For Each p In s.Points
            If p.HasDataLabel Then
                p.DataLabel.Formula = Replace(p.DataLabel.Formula, FROM_STR, TO_STR)
            End If
        Next p
    
    End If
End Sub

Open in new window

0
 

Author Closing Comment

by:ejortberg
ID: 39727180
Fantastic - you saved my holiday break!
0
 
LVL 1

Expert Comment

by:Jon_Peltier
ID: 41652560
You may include a check for an actual formula

If Left$(p.DataLabel.Formula, 1) = "=" Then

Open in new window


since .Formula will return the data label's .Text if it isn't a "real" formula.
0

Featured Post

SuperAntiSpyware Licenses Discounted by 25% !

Exclusive offer to Experts Exchange Members!
Buy SuperAntiSpyware License(s) from us and save 25% on the regular purchase price.
- Includes Full SuperAntiSpyware Vendor Support Entitlements
- Your Subscription does not begin until you activate your license
- Buy for your friends

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Microsoft exchange 2010 issue 1 16
Small office AntiVirus 6 32
Excel not storing numeric string correctly 5 34
copy down array 24 26
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.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

740 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