VBA将函数公式内的范围replace为其计算值

这是我在此的头一篇博文。 我试图寻找答案,但找不到一个答案。

我的问题是用公式中的范围代替其具体的值。 为了说明这个问题考虑一个单元格:

= AVERAGE(A1:A10)

我想要这个单元格显示计算的平均参数:

= AVERAGE({1; 2; 3; 4; 5; 6; 7; 8; 9; 10})

这个东西是平行的按下F9,而范围A1:A10突出显示。 我试图从网上find的东西,但无法使其工作:

Sub ChangeFormulas() Const CONST_FUNCTION As String = "AVERAGE" Dim cell As Range Dim tmp As String Dim res As Variant For Each cell In Selection With cell If InStr(.Formula, CONST_FUNCTION) > 0 Then tmp = Mid(.Formula, 10, InStr(.Formula, ")") - (InStr(.Formula, CONST_FUNCTION) + 8)) cell.Formula = Replace(cell.Formula, tmp, Application.Evaluate(tmp)) End If End With Next cell End Sub 

感谢您的帮助。

不pipe怎么样,按照@Brandon Barney,但这里是你所需要的

"=AVERAGE({" & join(application.transpose(range("i1:i4").Value),";") & "})"

哪个会像这样回来

=AVERAGE({1;5;5;10})