开发者

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

开发者 https://www.devze.com 2023-02-24 22:22 出处:网络
Please view开发者_JS百科 this image to get my clearly question: 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

Please view开发者_JS百科 this image to get my clearly question:

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


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


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.


If you don't mind extra clutter in your workbook you can fill four cells with the following:

4/18/11     0
4/18/11    90

Add a new series to the chart with this data. If you don't want the new series to show up in the chart legend, then double-click on the red series name in the legend and press 'Delete'.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号