Forum Discussion
Overlapping labels in stacked bar charts
NikolinoDE Thanks for sharing! I'm getting a "Run-time error '13' type mismatch error" when running this. It doesn't like this line: Set rng = ser.Points(i).DataLabel
Any ideas?
The "Run-time error '13' Type mismatch" error typically occurs when there is a mismatch between the data type expected by the code and the actual data type of the object being used. In this case, it is likely that the DataLabel property is not returning a valid Range object.
The DataLabel property for a Point in a chart series does not return a Range object; instead, it returns a DataLabel object. To fix the "Type mismatch" error, you should declare rng as a DataLabel object rather than a Range object.
Here is the modified code:
Sub AdjustDataLabels()
Dim cht As Chart
Dim ser As Series
Dim i As Long
Dim rng As DataLabel ' Declare rng as a DataLabel object
' 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
By declaring rng as a DataLabel object, you should be able to avoid the "Type mismatch" error when running the code.
If you have error again, please give with the error more information’s about your Excel version, operating system, storage medium. The proposed steps/solutions/Codes are untested. The text and steps were edited with the help of AI.
My answers are voluntary and without guarantee!
Hope this will help you.
Was the answer useful? Mark as best response and Like it!
This will help all forum participants.
- gdstew4Nov 30, 2023Copper ContributorI get a 483 error at line 19 with this version of the code. DataLabel does not contain the property Visible. I tried changing the property to ShowValue but then it gives another run-time error 13 at line 21.