Chart data label by color of text in cell

brandon20

Board Regular
Joined
Feb 12, 2014
Messages
203
Hi,

I was wondering if anyone could help me with some VBA code for a chart.

I need to put data labels on my chart, but I need the labels to reference a different range than the Data Series.
I have an add on called "XY chart Labels" that lets me do this, but I also need the data labels to match the text color and whether it is bold or underlined or all that in the cell. The XY chart Labels wont let me do this.
So I was wondering if someone could help me do this in VBA.

Here is an example, Lets say I have a bar chart of the Data Series but want to put data labels named by the Label range, Andd color the labels by the color of the text in the cell.
Thanks for any help

NameData SeriesLabel Range
Name110AA
Name215BB
Name320CC
Name430DD
Name540EE
Name650FF

<colgroup><col><col><col></colgroup><tbody>
</tbody>
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
The following macro assumes that the active sheet contains the data, starting at A1, and that it contains the chart...

Code:
[COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit[/COLOR]

[COLOR=darkblue]Sub[/COLOR] ApplyCustomLabels()

    [COLOR=darkblue]Dim[/COLOR] rLabels [COLOR=darkblue]As[/COLOR] Range
    [COLOR=darkblue]Dim[/COLOR] rCell [COLOR=darkblue]As[/COLOR] Range
    [COLOR=darkblue]Dim[/COLOR] oSeries [COLOR=darkblue]As[/COLOR] Series
    [COLOR=darkblue]Dim[/COLOR] Cnt [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR]
    
    [COLOR=darkblue]Set[/COLOR] rLabels = Range("C2:C" & Cells(Rows.Count, "C").End(xlUp).Row)
    
    [COLOR=darkblue]Set[/COLOR] oSeries = ActiveSheet.ChartObjects(1).Chart.SeriesCollection(1)
    
    oSeries.HasDataLabels = [COLOR=darkblue]True[/COLOR]
    
    Cnt = 1
    [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] rCell [COLOR=darkblue]In[/COLOR] rLabels
        [COLOR=darkblue]With[/COLOR] oSeries.Points(Cnt).DataLabel
            .Text = rCell.Value
            .Font.Color = rCell.Font.Color
        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
        Cnt = Cnt + 1
    [COLOR=darkblue]Next[/COLOR] rCell
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

To refer to a specific chart on the sheet, let's say "Chart 1", replace...

Code:
Set oSeries = ActiveSheet.ChartObjects(1).Chart.SeriesCollection(1)

with

Code:
Set oSeries = ActiveSheet.ChartObjects([COLOR=#ff0000]"Chart 1"[/COLOR]).Chart.SeriesCollection(1)

Hope this helps
 
Upvote 0
Amazing, yes this works great! Thank you

I am currently trying to figure out how to modify the code so it can color more an just one specific range, but I am a novice at this.

What if my Data actually looked like this, and so on, to data series 3, and 4 and 5....


NameData SeriesLabel RangeData Series2Label Range2
Name110AA20GG
Name215BB50DSF
Name320CC60asdf
Name430DD90gfdg
Name540dasf20hdf
Name650FF20ad

<colgroup><col><col><col><col><col></colgroup><tbody>
</tbody>


Is there a way to modify the code to look in more than just C2:C but also E2:E and so on..

again, Thanks!
 
Upvote 0
Try...

Code:
[COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit[/COLOR]

[COLOR=darkblue]Sub[/COLOR] ApplyCustomLabels()

    [COLOR=darkblue]Dim[/COLOR] rLabelRange [COLOR=darkblue]As[/COLOR] Range
    [COLOR=darkblue]Dim[/COLOR] rCell [COLOR=darkblue]As[/COLOR] Range
    [COLOR=darkblue]Dim[/COLOR] oSeries [COLOR=darkblue]As[/COLOR] Series
    [COLOR=darkblue]Dim[/COLOR] ColIndx [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] SrsIndx [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] PntIndx [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR]
    
    [COLOR=darkblue]Set[/COLOR] rLabelRange = Range("B2:E" & Cells(Rows.Count, "A").End(xlUp).Row) [COLOR=#008000]'adjust the ending column reference accordingly[/COLOR]
    
    SrsIndx = 1
    [COLOR=darkblue]For[/COLOR] ColIndx = 1 [COLOR=darkblue]To[/COLOR] rLabelRange.Columns.Count [COLOR=darkblue]Step[/COLOR] 2
        [COLOR=darkblue]Set[/COLOR] oSeries = ActiveSheet.ChartObjects(1).Chart.SeriesCollection(SrsIndx)
        oSeries.HasDataLabels = [COLOR=darkblue]True[/COLOR]
        PntIndx = 1
        [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] rCell [COLOR=darkblue]In[/COLOR] rLabelRange.Offset(, ColIndx).Resize(, 1).Cells
            [COLOR=darkblue]With[/COLOR] oSeries.Points(PntIndx).DataLabel
                .Text = rCell.Value
                .Font.Color = rCell.Font.Color
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
            PntIndx = PntIndx + 1
        [COLOR=darkblue]Next[/COLOR] rCell
        SrsIndx = SrsIndx + 1
    [COLOR=darkblue]Next[/COLOR] ColIndx
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

Hope this helps!
 
Upvote 0
Hi,

Thanks! that works. My only issue is that in my actual data table the series starts on column K, (as opposed to B). And the label range is 2 columns before (column I) as opposed to 1 column in front (column C).
I tried to use PntIndex =PntIndex +1 and change it to PntIndex =PntIndex - 2 but that did not work.
And Im realizing there are other issues as well, (that I dont want to label every range.)

Because of all these issues I will probably use the first code you gave and just adjust it to label the columns I need.

Thanks again.
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top