基本上,我不是从单元格中选择一个范围,而是通过使用循环将值存储在数组中。我理想中想做的是将这些数组用作 LinEst 函数中已知的 x 和 y。
这样做的目的并不重要,因为我想做的只是我已经编写的代码的一部分。然而,Do 循环(至少是第二个)确实需要存在,因为我尝试应用它的代码需要它们才能运行。
下面是我正在尝试编写的代码的简单示例。
Sub Test()
Dim Counter As Long
Dim Counter_1 As Long
Dim x As Single
Dim y As Single
Dim i As Single
Dim m As Single
Dim myArray_1() As Single
Dim myArray_2() As Single
ReDim myArray_1(i)
ReDim myArray_2(i)
Counter = 2
Counter_1 = 2
i = 0
Cells(1, 4) = "m"
x = Cells(Counter, 1)
y = Cells(Counter, 2)
Do
Do Until x = 0
myArray_1(i) = x
myArray_2(i) = y
Cells(Counter, 6) = myArray_1(i)
Cells(Counter, 7) = myArray_2(i)
i = i + 1
Counter = Counter + 1
x = Cells(Counter, 1)
y = Cells(Counter, 2)
ReDim Preserve myArray_1(i)
ReDim Preserve myArray_2(i)
Loop
m = WorksheetFunction.LinEst(myArray_2, myArray_1)
Cells(Counter_1, 4) = m
Loop
End Sub
所以基本上我希望 LinEst 函数使用每个数组作为已知的 y 和已知的 x。根据我所做的更改,我会收到不同的错误,例如“类型不匹配”或“无法获取工作表函数类的 LinEst 属性”。不管怎样,到目前为止我还没有运气让它工作,而且总是出错。从 LinEst 函数中,我想要的只是梯度“m”。
将内容放入单元格的唯一原因是确保代码按照我的要求执行。
根据我在互联网上的了解,可以在 LinEst 函数中使用数组,但是这些示例通常与我想要做的完全不同。
如果有人能提供帮助,我将不胜感激。先感谢您。有任何问题请随时询问我(们)。