Auto create line from earliest to latest date in range

Corleone

Well-known Member
Joined
Feb 2, 2003
Messages
836
Office Version
  1. 365
is there a way of getting excel to automatically generate a line that spans from the earliest date to the latest date - (see attached)
I suspect this one might be a bridge to far

I am currently using the formula in the gannt chart area with the characters set up in the conditioning formating
=FILTER(CHAR({186,190,191,192,187}),(G$2<=$B4:$F4)*(G$2>$B4:$F4-($H$2-$G$2)),"")

however im having to put the line in manually

any ideas appreciated
Thanks
Untitled.png
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
You can use two helper columns that will locate the start and end dates, then use code to draw the lines.

1662206664155.png



VBA Code:
Sub MakeLines()
    Dim sh As Worksheet
    Dim rng As Range, c As Range
    Dim m As Date, n As Date
    Dim mx As Range, nx As Range
    Dim dRng As Range
    Dim col As Long
    Dim LRng As Range
    Dim L, T, W, H
    Dim shp As Shape

    Set sh = ActiveSheet

    With sh
        For Each shp In .Shapes
            On Error Resume Next
            If shp.Name Like "*Line*" Then
                shp.Delete
            End If
            On Error GoTo 0
        Next
        col = .Cells(2, .Columns.Count).End(xlToLeft).Column
        Set dRng = .Range(.Cells(2, "G"), .Cells(2, col))
        Set rng = .Range("A4:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
        For Each c In rng.Cells
            m = c.Offset(, 1).Value
            n = c.Offset(, 2).Value
            Set mx = dRng.Find(DateValue(m), LookIn:=xlFormulas, LookAt:=xlPart)
            Set nx = dRng.Find(DateValue(n), LookIn:=xlFormulas, LookAt:=xlPart)
            Set LRng = .Range(.Cells(c.Row, mx.Column), .Cells(c.Row, nx.Column))
            'LRng.Select
            With LRng
                L = .Left
                T = .Top
                W = .Width
                H = .Height
            End With
           
            Set shp = .Shapes.AddConnector(msoConnectorStraight, L, _
                                           T + H / 2, L + W, T + H / 2)
            With shp
                .Name = "Line " & c.Row
            End With
        Next


    End With

End Sub

See attached sample, download to excel
Hide column B & C so nobody sees it.

 
Upvote 0
You can use two helper columns that will locate the start and end dates, then use code to draw the lines.

View attachment 73151


VBA Code:
Sub MakeLines()
    Dim sh As Worksheet
    Dim rng As Range, c As Range
    Dim m As Date, n As Date
    Dim mx As Range, nx As Range
    Dim dRng As Range
    Dim col As Long
    Dim LRng As Range
    Dim L, T, W, H
    Dim shp As Shape

    Set sh = ActiveSheet

    With sh
        For Each shp In .Shapes
            On Error Resume Next
            If shp.Name Like "*Line*" Then
                shp.Delete
            End If
            On Error GoTo 0
        Next
        col = .Cells(2, .Columns.Count).End(xlToLeft).Column
        Set dRng = .Range(.Cells(2, "G"), .Cells(2, col))
        Set rng = .Range("A4:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
        For Each c In rng.Cells
            m = c.Offset(, 1).Value
            n = c.Offset(, 2).Value
            Set mx = dRng.Find(DateValue(m), LookIn:=xlFormulas, LookAt:=xlPart)
            Set nx = dRng.Find(DateValue(n), LookIn:=xlFormulas, LookAt:=xlPart)
            Set LRng = .Range(.Cells(c.Row, mx.Column), .Cells(c.Row, nx.Column))
            'LRng.Select
            With LRng
                L = .Left
                T = .Top
                W = .Width
                H = .Height
            End With
          
            Set shp = .Shapes.AddConnector(msoConnectorStraight, L, _
                                           T + H / 2, L + W, T + H / 2)
            With shp
                .Name = "Line " & c.Row
            End With
        Next


    End With

End Sub

See attached sample, download to excel
Hide column B & C so nobody sees it.

Hi - I just had a quick look at it - The formulas work fine
but when i run the macro i fails at the following line
Set LRng = .Range(.Cells(c.Row, mx.Column), .Cells(c.Row, nx.Column))

I tried changing "Set rng = .Range("A4:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)" to A5 as this is the row where the dates begin but it still falls down at the line above

Cheers
 
Upvote 0
If it works with the sample I gave you but does not work with your sample. Then you would have to supply a sample to look at.
 
Upvote 0

Forum statistics

Threads
1,214,389
Messages
6,119,232
Members
448,879
Latest member
VanGirl

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