Overflow error when adjusting chart datalabel

Enigmatic999

New Member
Joined
Dec 20, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I am trying to adjust datalabel position of a clustered column chart to remove the overlap but get an overflow error at the line 'l2 = CInt(otherRng.Left)'. Below is the code

It works first time I run it. It also works when I call the sub outside the main VBA program which does many other things. Tried On Error. Tried Watch. Fully declared Data types and objects. Played around with CLng CDbl. I am using CInt as I do not want the precision but also tried it without this. There seems to be some delay before Datalabel position can be read. Possibly a chart rendering issue. The Chart is created beforehand on the sheet and I only dynamically alter the dataset. I have tried Application.Wait and Do Events. As part of the main program I have tried it before and after Application.Screenupdating and Application.Calculation.

VBA Code:
Sub AdjustDataLabels(ws)
    Dim i As Long, v As Long
    Dim vChk As Long, idx As Long
    Set cht = ws.ChartObjects("Chart 2").Chart
    vNo = ws.Range("B3")
    For v = 1 To vNo 'No of vendors
        For i = 1 To 3 'cycle through each delay category
            'cht.SeriesCollection(i).ApplyDataLabels
            Set rng = cht.SeriesCollection(i).Points(v).DataLabel
            chk1 = i * v
            If chk1 <> 1 And chk1 <> 3 * vNo Then
                vChk = v - 1 * (i = 3)
                idx = -(2 * (i = 1) + 3 * (i = 2) + 1 * (i = 3))
                Set otherRng = cht.SeriesCollection(idx).Points(vChk).DataLabel
                l2 = CInt(otherRng.Left)
                t2 = CInt(otherRng.Top)
                h2 = CInt(otherRng.Height)
                w2 = CInt(otherRng.Width)
                Do While CheckOverlap(l2, t2, h2, w2, rng)
                    rng.Top = rng.Top - 5
                Loop
                vChk = v + 1 * (i = 1)
                idx = -(3 * (i = 1) + 1 * (i = 2) + 2 * (i = 3))
                Set otherRng = cht.SeriesCollection(idx).Points(vChk).DataLabel
                l2 = CInt(otherRng.Left)
                t2 = CInt(otherRng.Top)
                h2 = CInt(otherRng.Height)
                w2 = CInt(otherRng.Width)
                Do While CheckOverlap(l2, t2, h2, w2, rng)
                    rng.Top = rng.Top - 5
                Loop
            End If
            If i = 1 Or i = 3 Then
                vChk = v
                idx = -(3 * (i = 1) + 1 * (i = 3))
                Set otherRng = cht.SeriesCollection(idx).Points(vChk).DataLabel
                l2 = CInt(otherRng.Left)
                t2 = CInt(otherRng.Top)
                h2 = CInt(otherRng.Height)
                w2 = CInt(otherRng.Width)
                Do While CheckOverlap(l2, t2, h2, w2, rng)
                    rng.Top = rng.Top - 5
                Loop
            End If
        Next
    Next
End Sub

Function CheckOverlap(l2, t2, h2, w2, rng)
    l1 = CInt(rng.Left)
    t1 = CInt(rng.Top)
    h1 = CInt(rng.Height)
    w1 = CInt(rng.Width)
    'On Error GoTo skip
    If Not ((l1 + w1) < l2 Or _
            l1 > (l2 + w2) Or _
            (t1 + h1) < t2 Or _
            t1 > (t2 + h2)) Then
        CheckOverlap = True
        Exit Function
    End If
skip:
    CheckOverlap = False
End Function

Below is the Chart

Picture1.png
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
After much trial and error and many red-herrings (even had the top position showing as nan and inf) I found the answer to be selecting the chart. Wanted to avoid selecting the chart which is on a different sheet which also then had to be selected. So it means that datalabel position change has to have the sheet/chart selected first. Not sure if this is because of what I was doing or whether this must be the case everytime.
 
Upvote 0
Solution

Forum statistics

Threads
1,224,815
Messages
6,181,136
Members
453,021
Latest member
Justyna P

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