gpt4 book ai didi

arrays - 可以在 VBA 的 LinEst 函数中使用数组吗?

转载 作者:行者123 更新时间:2023-12-05 01:05:34 25 4
gpt4 key购买 nike

基本上,我通过使用循环将值存储在数组中,而不是从单元格中选择一个范围。理想情况下,我想做的是在 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 函数中使用数组,但是这些示例通常与我想要做的完全不同。

如果有人可以提供帮助,我将是最伟大的。先感谢您。有任何问题请随时询问我(们)。

最佳答案

是的,这是可以做到的。下面的代码片段应该可以帮助您入门:

    Dim x() As Variant
ReDim x(1 To 3)
x(1) = 1
x(2) = 2
x(3) = 3

Dim y() As Variant
ReDim y(1 To 3)
y(1) = 4
y(2) = 5
y(3) = 6

Dim z() As Variant
z = WorksheetFunction.LinEst(x, y)

该函数返回 Variant其中“装箱”了 Variant 的数组(这将是一维或二维的)。其他两个参数(上面未显示)是 True 或 False。该函数在 Excel 帮助中另有详细说明。

关于arrays - 可以在 VBA 的 LinEst 函数中使用数组吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21267540/

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