在 Excel 中计算移动平均线
我想计算一列最后(例如 20)个数字的移动平均值。问题是该列的某些单元格可能为空,应忽略它们。示例:
A
175
154
188
145
155
167
201
最后三个的移动平均值为 (155+167+201)/3。我尝试使用平均值、偏移量、索引来实现此目的,但我根本不知道如何实现。我对宏有点熟悉,所以这样的解决方案可以很好地工作:=MovingAverage(A1;3)
感谢您提供任何提示或解决方案!
I want to calculate a moving average of the last, say 20, numbers of a column. A problem is that some of the cells of the column may be empty, they should be ignored. Example:
A
175
154
188
145
155
167
201
A moving average of the last three would be (155+167+201)/3. I've tried to implement this using average, offset, index, but I simply don't know how. I'm a little bit familiar with macros, so such a solution would work fine: =MovingAverage(A1;3)
Thanks for any tips or solutions!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 control+shift+enter 输入使其成为数组公式。这将找到最新的三个值。如果您想要更多或更少,请将公式中“3”的两个实例更改为您想要的任何值。
此部分返回具有值的所有单元格中第四高的行号,或者在示例中返回 5,因为第 6、8 和 9 行是具有值的第一到第三高的行。
该部分根据行号是否大于第四大行号返回 9 个 TRUE 或 FALSE。
这会将 A1:A9 中的值乘以这 9 个 TRUE 或 FALSE。 TRUE 转换为 1,FALSE 转换为 0。这留下了一个像这样的 SUM 函数
因为所有高于 155 的值都不满足行数标准,所以 get 乘以零。
Enter this with control+shift+enter to make it an array formula. This will find the latest three values. If you want more or less, change the two instances of '3' in the formula to whatever you want.
This part returns the 4th highest row number of all the cells that have a value, or 5 in your example because rows 6, 8, and 9 are the 1st through 3rd highest rows with a value.
This part returns 9 TRUEs or FALSEs based on whether the row number is larger than the 4th largest.
This multiplies the values in A1:A9 by those 9 TRUEs or FALSEs. TRUEs are converted to 1 and FALSEs to zero. This leaves a SUM function like this
Because all the values above 155 don't satisfy the row number criterion, the get multiplied by zero.
如果您要使用 UDF,则只有在参数包含您要处理的所有数据范围时,它才会在您更改数据时正确重新计算。
这是一个处理整个列并包含一些错误处理的移动平均 UDF。
您可以通过在单元格中输入公式
=MovingAverage(A:A,3)
来调用它。If you are going to use a UDF it will only recalculate correctly when you change the data if the parameters include all the range of data you want to handle.
Here is a moving average UDF that handles entire columns and contains some error handling.
You can call it using by entering the formula
=MovingAverage(A:A,3)
into a cell.只是一个快速解决方案:
假设您的数字位于单元格 A2:A10 上,请在 B10 中输入以下公式:
向上拖动公式即可得到移动平均值
如果存在两个连续空白的可能性,您可以嵌套另一个 if,超过这个值,此解决方案就变得太复杂的
Just a quick solution:
Supposing your numbers are on the cells A2:A10, put in B10 the following formula:
Dragging up the formula you get the moving average
If there is the possibility of two consecutive blank you could nest another if, more than that and this solution became too complicated
我用 VBA 写了一个简短的脚本。希望它能达到你想要的效果。在这里:
1) 我已将限制设置为 360 个单元格。这意味着该脚本不会查找超过 360 个单元格。如果您想更改它,请更改计数器的初始值。
2) 脚本返回未四舍五入的平均值。将最后一行更改为
MovingAverage = Round(CDbl(tmp / i),2)
3) 使用方法如你所愿,只需输入 =MovingAverage("a1";3) 即可细胞。
欢迎任何评论。
I have written a short script in VBA. Hopefull it does what you want. Here you are:
1) I have set limit to 360 cells. It means that the script will not look for more than 360 cells. If you want to change it then change the initial value of counter.
2) The script returns not rounded average. Change the last row to
MovingAverage = Round(CDbl(tmp / i),2)
3) The use is just like you wanted, so just type =MovingAverage("a1";3) into the cell.
Any comments are welcome.