Enigmatic999
New Member
- Joined
- Dec 20, 2024
- Messages
- 2
- Office Version
- 365
- Platform
- 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.
Below is the Chart
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