使用macros在Excel图表中创build水平线

在工作中,我已经制定了一个macros来格式化并绘制出testing我们所制造零件的数据。 这些值必须在一定的范围内,所以我想在图表中添加表示公差范围的线条。

例如,一个参数是某个信号的电压值。 它必须介于.271和.451之间,因此我希望在该图表的那些值上添加行。

其他图表测量不同的东西,会有不同的值,但基本原理是一样的。

每个信号的数据点的数量是不一样的,但通常是相当大的,每个数千左右。

我在互联网上发现了一些涉及绘图工具的不同选项,或者为图表添加了一个新的数据系列,但是我不太熟悉excelmacros的这些方面。 实际上,我不认为我能find我第一次find数据系列思想的页面。

每条线的新系列是最好的方法。

'add a line series with line but no markers Sub AddLineSeries(cht As ChartObject, sName As String, xvals, _ yvals, SeriesColor As Long) Dim s As Series Set s = cht.Chart.SeriesCollection.NewSeries With s .Name = sName .Values = yvals .XValues = xvals .MarkerBackgroundColor = xlNone .MarkerForegroundColor = SeriesColor .MarkerStyle = xlMarkerStyleNone With .Border .Weight = xlThin .Color = SeriesColor End With End With End Sub 

用法(为每个截止点添加一行):

 'cht is the chart object 'minX/maxX are x-axis values you want to plot the line for 'qcMin/Max are the y-axis values for your lower/upper cut-offs 'Array() just creates an array of values to pass to the chart for plotting, since ' we're not using values from a worksheet for this series AddLineSeries cht, "QC Min", Array(minX, maxX), Array(qcMin, qcMin), _ RGB(255, 0, 0) AddLineSeries cht, "QC Max", Array(maxX, maxX), Array(qcMax, qcMax), _ RGB(255, 0, 0) 

假设您正在使用XY散点图,只需创build一对数据点并将其添加为新系列。 例如,input您的数据,如A1:B6所示,然后将整个范围添加为一系列。 你会得到两行。 显然,X和Y值应该从原始数据中计算出来。

在这里输入图像说明