farmerscott
Well-known Member
- Joined
- Jan 26, 2013
- Messages
- 824
- Office Version
- 365
- Platform
- Windows
Hi,
I am trying to get a code to draw an arrow/line between 2 cells ("C4" and "F14" in this example).
Using the following code from Draw Line From Center Of Cell To Center Of Another - OzGrid Free Excel/VBA Help Forum
However, when I run the code, the line does not-
1. come from the middle of cell "C4", it is more from the bottom right of the cell.
2. end at cell "F14" it is in the bottom middle of cell "G16".
I have also tried the following code and it draws the exact same start and end points.
Source: Draw Excel Lines or Arrows Between Cells with VBA - wellsr.com
Both codes seem logical in their calculations........
Both codes allow for changes in the width of columns........
What am i missing?
Thanks, FarmerScott
I am trying to get a code to draw an arrow/line between 2 cells ("C4" and "F14" in this example).
Using the following code from Draw Line From Center Of Cell To Center Of Another - OzGrid Free Excel/VBA Help Forum
VBA Code:
Sub Macro2()
Dim lbx As Long
Dim lby As Long
Dim lex As Long
Dim ley As Long
With Range("C4")
lbx = .Left + .Width / 2
lby = .Top + .Height / 2
End With
With Range("F14")
lex = .Left + .Width / 2
ley = .Top + .Height / 2
End With
Sheet1.Shapes.AddLine lbx, lby, lex, ley
'expression.AddLine(BeginX, BeginY, EndX, EndY)
End Sub
However, when I run the code, the line does not-
1. come from the middle of cell "C4", it is more from the bottom right of the cell.
2. end at cell "F14" it is in the bottom middle of cell "G16".
I have also tried the following code and it draws the exact same start and end points.
VBA Code:
Sub DrawArrows3()
Dim FromRange As Range
Dim ToRange As Range
Dim dleft1 As Double, dleft2 As Double
Dim dtop1 As Double, dtop2 As Double
Dim dheight1 As Double, dheight2 As Double
Dim dwidth1 As Double, dwidth2 As Double
Set FromRange = Range("C4")
Set ToRange = Range("F14")
dleft1 = FromRange.Left
dleft2 = ToRange.Left
dtop1 = FromRange.Top
dtop2 = ToRange.Top
dheight1 = FromRange.Height
dheight2 = ToRange.Height
dwidth1 = FromRange.Width
dwidth2 = ToRange.Width
ActiveSheet.Shapes.AddConnector(msoConnectorStraight, dleft1 + dwidth1 / 2, dtop1 + dheight1 / 2, dleft2 + dwidth2 / 2, dtop2 + dheight2 / 2).Select
'format line
With Selection.ShapeRange.Line
.BeginArrowheadStyle = msoArrowheadNone
.EndArrowheadStyle = msoArrowheadOpen
.Weight = 1.5
.Transparency = 0.5
.BeginArrowheadStyle = msoArrowheadTriangle
.EndArrowheadStyle = msoArrowheadTriangle
.ForeColor.RGB = RGB(0, 0, 255)
End With
End Sub
Both codes seem logical in their calculations........
Both codes allow for changes in the width of columns........
What am i missing?
Thanks, FarmerScott