Overlapping labels in stacked bar charts

Copper Contributor

Hey,

 

I am facing the issue of overlapping data labels visible for illustrations. I want to avoid doing a manual solution since I wont be the only user of this spreadsheet. 

 

I have been trying different VBA codes to get around the problem, but often run into the "438 overflow" error.

 

Does anyone have any advice on how to write a VBA code which automatically separates overlapping data labels when using the "Refresh data" function?

 

All help is appreciated!

 

Br

Aram

1 Reply

@AramA92 

To automatically avoid overlapping data labels in stacked bar charts in Excel using VBA, you can create a macro that adjusts the label positions.

Here is an example of a VBA code that does this:

Sub AdjustDataLabels()
'This code is only an example, you may have to adapt it to your needs.
    Dim cht As Chart
    Dim ser As Series
    Dim i As Long
    Dim rng As Range
    
    ' Set the chart object
    Set cht = ActiveSheet.ChartObjects(1).Chart
    
    ' Loop through all series in the chart
    For Each ser In cht.SeriesCollection
        ' Check if the series has data labels
        If ser.HasDataLabels Then
            ' Loop through each point in the series
            For i = 1 To ser.Points.Count
                ' Set the range for the data label
                Set rng = ser.Points(i).DataLabel
                ' Check if the data label is visible
                If rng.Visible Then
                    ' Check for overlap with other data labels
                    Do While CheckOverlap(ser, i, rng)
                        ' Adjust the position of the data label
                        rng.Left = rng.Left + 5 ' You can adjust this value as needed
                    Loop
                End If
            Next i
        End If
    Next ser
End Sub

Function CheckOverlap(ser As Series, index As Long, rng As Range) As Boolean
    Dim i As Long
    Dim otherRng As Range
    
    ' Loop through other points in the series
    For i = 1 To ser.Points.Count
        If i <> index Then
            ' Get the data label of the other point
            Set otherRng = ser.Points(i).DataLabel
            ' Check if the data labels overlap
            If Not (rng.Left + rng.Width < otherRng.Left Or _
                    rng.Left > otherRng.Left + otherRng.Width Or _
                    rng.Top + rng.Height < otherRng.Top Or _
                    rng.Top > otherRng.Top + otherRng.Height) Then
                ' Overlap detected
                CheckOverlap = True
                Exit Function
            End If
        End If
    Next i
    ' No overlap detected
    CheckOverlap = False
End Function

This VBA code iterates through each series in the chart, then through each data point in each series. For each data point, it checks if the data label is visible and if it overlaps with other data labels. If an overlap is detected, it adjusts the position of the data label to the right (you can adjust the value rng.Left + 5 to control the adjustment).

To use this code:

  1. Open your Excel workbook with the stacked bar chart.
  2. Press ALT + F11 to open the VBA editor.
  3. Insert a new module (right-click on "Modules" in the Project Explorer and choose "Insert" > "Module").
  4. Paste the code into the module.
  5. Close the VBA editor.
  6. Run the macro by pressing ALT + F8, selecting "AdjustDataLabels," and clicking "Run."

This code should help automatically adjust data labels to avoid overlap when refreshing data in your stacked bar chart.The text, steps and the code were created with the help of AI.

My answers are voluntary and without guarantee!

 

Hope this will help you.