excel图表制作:给多个数据系列添加趋势线的加载宏
时间:2022-12-18 07:11:51
我们手工给多个散点图系列添加了一条趋势线,如下图 1 所示。
图 1
在图表中,最开始有 3 个数据系列,其公式分别为:
=系列(Sheet1!$ C $ 2,Sheet1!$ B $ 3:$ B $ 11,Sheet1!$ C $ 3:$ C $ 11,1)
=系列(Sheet1!$ E $ 2,Sheet1!$ D $ 3:$ D $ 11,Sheet1!$ E $ 3:$ E $ 11,2)
=系列(Sheet1!$ G $ 2,Sheet1!$ F $ 3:$ F $ 11,Sheet1!$ G $ 3:$ G $ 11,3)
注意,图表的系列公式一般有 4 个参数:
= SERIES(系列名称,X 值,Y 值,绘图顺序值)
后来,我们添加了一个所谓的“趋势线”的新系列,合并了原来的 3 个系列的 X 值和 Y 值,其公式为:
= SERIES(“ 趋势线”,
(Sheet1!$ B $ 3:$ B $ 11,Sheet1!$ D $ 3:$ D $ 11,Sheet1!$ F $ 3:$ F $ 11),
(Sheet1!$ C $ 3:$ C $ 11,Sheet1!$ E $ 3:$ E $ 11,Sheet1!$ G $ 3:$ G $ 11),
4)
组成 X 值和 Y 值的多个单元格区域被包含在括号中。
下面,我们使用 VBA 代码来自动添加趋势线。
代码执行的操作是对图表中的系列进行计数,依次读取每个系列公式,分解其参数,然后将单独的 X 和 Y 值连接为组合的 X 和 Y 值。接着,代码将应用系列公式的参数添加新系列,隐藏标记并添加趋势线。
Sub ComputeMultipleTrendline()
If Not ActiveChart Is Nothing Then
With ActiveChart
Dim ixSeries As Long
For ixSeries = 1 To .SeriesCollection.Count
Dim SeriesFormula As String
SeriesFormula = ActiveChart.SeriesCollection(ixSeries).Formula
SeriesFormula = Mid$(SeriesFormula, InStr(SeriesFormula, “(“)+ 1)
SeriesFormula = Left$(SeriesFormula, Len(SeriesFormula) – 1)
Dim SeriesArgs As Variant
SeriesArgs = Split(SeriesFormula, “,”)
Dim XAddress As String, YAddress As String
XAddress = XAddress & SeriesArgs(LBound(SeriesArgs) + 1) &”,”
YAddress = YAddress & SeriesArgs(LBound(SeriesArgs) + 2) &”,”
Next
XAddress= “=(” & Left$(XAddress, Len(XAddress) – 1) & “)”
YAddress= “=(” & Left$(YAddress, Len(YAddress) – 1) & “)”
With ActiveChart.SeriesCollection.NewSeries
.Name =”趋势线”
.XValues = XAddress
.Values= YAddress
.Format.Line.Visible = False
.MarkerStyle = xlMarkerStyleNone
With.Trendlines.Add.Format.Line
.DashStyle = msoLineSolid
.ForeColor.ObjectThemeColor = msoThemeColorText1
.ForeColor.Brightness= 0
EndWith
End With
End With
End If
End Sub
在工作表中选取图表,运行上述代码,可以看到图表中自动添加了一条趋势线。
安装该加载宏后, Excel 功能会添加一个称为“ Multi Trendline” 的选项卡,带有一个称为“ Multi ScatterTrendline ”的命令按钮,如下图 2 所示。
图 2
在工作表中选择图表,运行“ Multi Scatter Trendline”命令,会弹出一个对话框,可以选择要添加趋势线的图表数据系列前的标记,如下图 3 所示。
图 3
选择要添加趋势线的数据系列后,依次“确定”按钮,图表中会自动绘制一条趋势线,如下图 4 所示。
图 4
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何禁用word中的另存为?
Excel2003单元格填充技巧
![](https://img.aspxhome.com/file/2023/2/37992_0s.jpg)
iOS 14 beta 6 修复搜索框不显示 Siri 建议的问题:如何再次隐藏?
![](https://img.aspxhome.com/file/2023/3/a194673_0s.png)
Excel如何一次性打开多个工作簿
![](https://img.aspxhome.com/file/2023/9/37159_0s.jpg)
excel表格中姓名对齐
![](https://img.aspxhome.com/file/2023/6/55086_0s.gif)
如何恢复Word快捷键初始设置?
![](https://img.aspxhome.com/file/2023/8/29928_0s.jpg)
Word文档文本框透明怎么设置?文本框如何配置才能无边框?
![](https://img.aspxhome.com/file/2023/8/31518_0s.jpg)
在Word文档中怎么插入公差?
![](https://img.aspxhome.com/file/2023/7/25007_0s.jpg)
word红头文件.公文的排版技巧
![](https://img.aspxhome.com/file/2023/5/21455_0s.gif)
excel快速查看局部数据,快速删除表框线,快速填充颜色,快速选中一列或行数据
![](https://img.aspxhome.com/file/2023/6/a140556_0s.jpg)
EXCEL数组公式怎么使用? EXCEL从入门到精通的技巧大全
![](https://img.aspxhome.com/file/2023/0/a141760_0s.jpg)
看看Excel的那些常用对象(3)
![](https://img.aspxhome.com/file/2023/2/a141962_0s.png)
Word自动断字功能介绍使用方法
![](https://img.aspxhome.com/file/2023/5/20945_0s.jpg)
w10开机蓝屏0xc000001怎么办?w10开机蓝屏0xc000001教程
![](https://img.aspxhome.com/file/2023/3/48073_0s.jpg)
Excel金额小写转大写公式
Excel怎么让单元格形状更具特点?
![](https://img.aspxhome.com/file/2023/5/41865_0s.jpg)
wps页面设置在哪?
![](https://img.aspxhome.com/file/2023/1/a164261_0s.png)
Word2019书法加载项在哪 Word加载项不兼容、变成灰色如何解决
![](https://img.aspxhome.com/file/2023/5/24715_0s.jpg)
Excel 对合并计算的编辑
excel重复数据标红的方法
![](https://img.aspxhome.com/file/2023/1/a140481_0s.png)