Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[Excel][VBA] How to draw a line in a graph?

Please view this image to get my clearly question: enter image description here

like image 987
hungbm06 Avatar asked Apr 15 '11 15:04

hungbm06


2 Answers

Sub Tester()
    Dim s, d

    d = #4/18/2011# * 1 ''a bit of a hack, since I could figure out how to plot a date directly
    With ActiveSheet.ChartObjects("Chart 1").Chart 'adjust to suit

        Set s = .SeriesCollection.NewSeries()
        With s
            .Name = ""
            .XValues = Array(d, d)
            .Values = Array(90, 0)
            .MarkerStyle = xlMarkerStyleNone
            .Border.Color = vbRed
        End With

    End With

End Sub
like image 156
Tim Williams Avatar answered Sep 27 '22 23:09

Tim Williams


Excellll's answer is incomplete. If you simply add this data to what is obviously a LINE chart, it will not appear where intended. You have to convert the added series to an XY chart series (right click on the series, Chart Type).

Also, your line falls midway between 4/17 and 4/18, so you need to use noon on 4/17 as the X value, that is 4/17/11 12:00.

Here is a set of articles about adding lines to Excel charts: http://peltiertech.com/Excel/Charts/AddLine.html

Also, deleting the legend entry is done by selecting the text of the legend entry and pressing Delete. This takes two single clicks on the legend entry, not one double click.

like image 21
Jon Peltier Avatar answered Sep 28 '22 01:09

Jon Peltier