gpt4 book ai didi

excel - 访问由 LinEst 函数产生的数组

转载 作者:行者123 更新时间:2023-12-04 20:39:16 26 4
gpt4 key购买 nike

我正在尝试使用以下代码从 lineEst 函数为我拥有的所有数据行输出 R2 的值:

Sub getdeflection6()
Dim xvalues() As Double, yvalues() As Double, cell As Range
Dim alldata As Range
Dim results As Variant
Dim counter As Integer

ReDim xvalues(0 To 8, 0 To 0)
ReDim yvalues(0 To 8, 0 To 0)
ReDim results(0 To 5, 0 To 6)

xvalues(0, 0) = 0
xvalues(1, 0) = Range("S2").Value
xvalues(2, 0) = Range("P2").Value
xvalues(3, 0) = Range("M2").Value
xvalues(4, 0) = Range("J2").Value
xvalues(5, 0) = Range("G2").Value
xvalues(6, 0) = Range("C2").Value
xvalues(7, 0) = Range("B2").Value

Set alldata = Range("F7", Range("F7").End(xlDown))

counter = 7
For Each cell In alldata

yvalues(0, 0) = 0
yvalues(1, 0) = cell.Offset(0, 15).Value
yvalues(2, 0) = cell.Offset(0, 12).Value
yvalues(3, 0) = cell.Offset(0, 9).Value
yvalues(4, 0) = cell.Offset(0, 6).Value
yvalues(5, 0) = cell.Offset(0, 3).Value
yvalues(6, 0) = cell.Value
yvalues(7, 0) = 0

results = Application.LinEst(yvalues, Application.Power(xvalues, Array(1, 2, 3, 4, 5)), True, True)

Cells(counter, 23) = results(2, 0)
counter = counter + 1

Next cell
End Sub

当我尝试访问 results(2,0) 数组以获取表示下标超出范围的 R2 值时,最后一行出现错误。我做错了吗?据我了解,结果数组将是 5 行 x 6 列?

最佳答案

当您将结果显示为 ReDim results(0 To 5, 0 To 6)它不会“锁定”LBoundUBound该变量数组的维度。

此行重置 结果进入结果(1 到 5、1 到 6)。

results = Application.LinEst(yvalues, Application.Power(xvalues, Array(1, 2, 3, 4, 5)), True, True)

事实上,原来的redim语句根本没有任何意义。使用新的 LBound(results, 1) 和 LBound(results, 2) 从数组中检索所需的值。
Cells(counter, 23) = results(2, 1)

我不确定其中的2。如果新的 LBound(results, 1) 为 1,则可能必须为 3。

关于excel - 访问由 LinEst 函数产生的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37150256/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com