Relative Chart Data Label Positioning

bs0d

Well-known Member
Joined
Dec 29, 2006
Messages
622
I have a clustered stacked column chart. The stacked columns are set to 0% gap width, so they actually appear side-by-side rather than on top of each other. This works wells for me, except when it comes to data labels. Because the series are defined as stacked, the option for "outside end" is not available. I would like for the data labels to appear just above each bar. I believe I need to incorporate a VBA routine that sets the label position above the value of the bar because the chart is also dynamic. That is, the data changes based on other selections I make in the workbook. So if I manually drag the labels above the value, they can be wildly out of proportion once the data changes.

Can you still use VBA to apply an outside end position for chart labels even if it's not available through the Excel GUI?

or - Can someone show me an example of a loop that will cycle through the chart series and each value, and set the label position a relative position above the series value, such as value + 5 pixels?

Please let me know if the background or request is unclear in any way.


Thanks,
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Here's what I've come up with. At this point, perhaps the community can provide feedback to optimize and make the code more efficient, or propose alternatives:

Code:
Sub ModDataLabels()

Application.ScreenUpdating = False
Dim newTop As Long

Sheets("__SHEET NAME___").ChartObjects("___CHART NAME___").Activate

'Remove existing labels:
ActiveChart.SetElement (msoElementDataLabelNone)
        
With ActiveChart
    For x = 1 To .SeriesCollection.Count
            
        'Add labels at known positioning for desired series:
        .FullSeriesCollection(x).HasDataLabels = True
        
        'Format DataLabels:
        With .FullSeriesCollection(x)
            .DataLabels.Position = xlLabelPositionInsideEnd
            .DataLabels.NumberFormat = "#,###;;;"
            .HasLeaderLines = False
            .DataLabels.Font.Bold = True
        End With
  
        'Adjust label position:
        For y = 2 To .FullSeriesCollection(x).Points.Count

            On Error Resume Next 'DataLabels don't exist for each point; skip where appropriate
            
            newTop = .FullSeriesCollection(x).Points(y).DataLabel.Top - 25
            
            'Apply new values:
            .FullSeriesCollection(x).Points(y).DataLabel.Top = newTop
        Next y
    Next x
End With

Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,924
Members
452,366
Latest member
TePunaBloke

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