[Excel][VBA] 如何在图形中画一条线?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5678667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:22:29  来源:igfitidea点击:

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

excelvbagraphexcel-vbadraw

提问by hungbm06

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

请查看此图片以了解我的明确问题: 在此处输入图片说明

回答by Tim Williams

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

回答by Jon Peltier

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).

Excelllll 的回答是不完整的。如果您只是将这些数据添加到明显是 LINE 图表的图表中,它就不会出现在预期的位置。您必须将添加的系列转换为 XY 图表系列(右键单击系列,图表类型)。

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.

此外,您的线位于 4/17 和 4/18 之间的中间,因此您需要使用 4/17 的中午作为 X 值,即 4/17/11 12:00。

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

这里有一组关于在 Excel 图表中添加线条的文章: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.

此外,通过选择图例条目的文本并按 Delete 键来删除图例条目。这需要在图例条目上单击两次,而不是一次双击。

回答by Excellll

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'.

使用此数据向图表添加一个新系列。如果您不希望新系列显示在图表图例中,请双击图例中的红色系列名称,然后按“删除”。