Originally Posted by
gibbo1715
I cant take the credit for that, it was given to me by Aaro Blood a VBAX regular
I made it smarter if you want to update your example.
Now it detects the position of range 1 in relation to range 2 and attaches the connectors more appropriately.
Function MyCon(Rng1 As Range, Rng2 As Range, Optional Color As Integer, Optional x As Double)
Dim MyShape As Shape, n1$, n2$, n3$
Dim L, T, W, H, BegCon, EndCon, rDiff, cDiff
Dim Vert As Boolean
'test for connector positioning
Vert = False
rDiff = (Rng1.Row - Rng2.Row)
cDiff = (Rng1.Column - Rng2.Column)
If Abs(cDiff) <= 1 Then Vert = True
If Vert Then
If rDiff <= 0 Then
BegCon = 3
EndCon = 1
End If
If rDiff >= 0 Then
BegCon = 1
EndCon = 3
End If
Else
If cDiff <= 0 Then
BegCon = 4
EndCon = 2
End If
If cDiff >= 0 Then
BegCon = 2
EndCon = 4
End If
End If
'Draw rounded rectangle object around Rng1
With Rng1
L = .Left
T = .Top
W = .Width
H = .Height
End With
Set MyShape = ActiveSheet.Shapes.AddShape(msoShapeRoundedRectangle, L, T, W, H)
n1$ = Chr(164) & Rng1.Address(False, False) & ":" & x
On Error Resume Next
With MyShape
.Name = n1$
End With
If Err <> 0 Then
MyShape.Delete
End If
On Error GoTo 0
Set Shape2 = ActiveSheet.Shapes(n1$)
With Shape2
.Fill.Visible = False
.Line.ForeColor.SchemeColor = Color
End With
'Draw rounded rectangle object around Rng2
With Rng2
L = .Left
T = .Top
W = .Width
H = .Height
End With
Set MyShape = ActiveSheet.Shapes.AddShape(msoShapeRoundedRectangle, L, T, W, H)
n2$ = Chr(164) & Rng2.Address(False, False) & ":" & x
On Error Resume Next
With MyShape
.Name = n2$
End With
If Err <> 0 Then
MyShape.Delete
End If
On Error GoTo 0
Set Shape2 = ActiveSheet.Shapes(n2$)
With Shape2
.Fill.Visible = False
.Line.ForeColor.SchemeColor = Color
End With
'connect the shapes with a line
Set MyShape = ActiveSheet.Shapes.AddConnector(msoConnectorStraight, L, T, W, H)
n3$ = n1$ & "-" & n2$
With MyShape
.Name = n3$
.Line.ForeColor.SchemeColor = Color
.Line.DashStyle = msoLineDash
.ConnectorFormat.BeginConnect ActiveSheet.Shapes(n1$), BegCon
.ConnectorFormat.EndConnect ActiveSheet.Shapes(n2), EndCon
End With
End Function