使用宏在 Excel 图表中创建水平线

发布于 2025-01-02 10:52:59 字数 321 浏览 1 评论 0原文

在工作中,我制作了一个宏来格式化和绘制来自测试我们制造的零件的数据。这些值必须在一定范围内,因此我想在图表中添加线条来指示公差限制的位置。

例如,一个参数是某一信号的电压值。它必须在 0.271 和 0.451 之间才能符合规格,因此我想在该图表上的这些值处添加线条。

其他图表测量不同的事物并且具有不同的值,但基本原理是相同的。

每个信号的数据点数量并不相同,但通常都很大,每个信号大约有几千个。

我在互联网上找到了几个不同的选项,涉及绘图工具或向图表添加新的数据系列,但我不太熟悉 Excel 宏的这些方面。事实上,我认为我找不到第一次找到数据系列想法的页面。

At work, I have made a macro to format and chart out data that comes from testing the parts we make. The values have to be within a certain range, so I would like to add lines to the charts denoting where the tolerance limits are.

For example, one parameter is a voltage value for a certain signal. It must be between .271 and .451 to be in spec, so I would like to add lines at those values on that chart.

Other charts are measuring different things and would have different values, but the basic principle is the same.

The number of data points per signal is not the same, but is usually quite large, around a couple thousand each.

I have found a couple of different options around the internet involving the drawing tools or adding a new data series to the charts, but I am not well versed in those aspects of excel macros. In fact, I don't think I can find the page that I first found the data series idea at.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

手心的温暖 2025-01-09 10:52:59

每条线推出一个新系列是最好的方法。

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

A new series for each line is the best approach.

 '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

Usage (adding one line for each cut-off):

'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)
若相惜即相离 2025-01-09 10:52:59

假设您使用的是 XY 散点图,只需创建几个数据点并将其添加为新系列。例如,输入 A1:B6 中所示的数据,然后将整个范围添加为一个系列。你会得到两条线。显然,X 和 Y 值应该根据原始数据计算得出。

在此处输入图像描述

Assuming you are using an XY Scatter chart, just create a couple of data points and add it as a new series. For example, enter your data as shown in A1:B6 and then add that entire range as a series. You'll get two lines. Obviously, the X and Y values should be calculated from your original data.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文