簡體   English   中英

需要幫助將數組從 VBA 返回到 Excel

[英]Need help returning an array from VBA into Excel

我想將compZ的值返回到我的 excel 數據表中。 如果你能告訴我怎么做,我將不勝感激。

代碼附在下面:

Option Explicit
Option Base 1

Public Function valZ120() As Double

'declare variables

Dim equation As Double
Dim x As Double
Dim randint As String
Dim compZ(476) As Double
Dim derEq As Double
Dim valX As Double
Dim nvalX As Double
Dim varA As Double
Dim varB As Double
Dim i As Double

'loop from i=25 to i=500

For i = 25 To 500
   varA = (0.538083613 * 13736.62195 * i) / ((8.3145 ^ 2) * (120 ^ 2))
   varB = (2.528160538 * i) / (8.3145 * 120)
   x = 0
   nvalX = 0.2

   'a Do While loop

   Do While Abs(nvalX - x) >= (0.01)
      x = nvalX
      equation = (x ^ 3) - (x ^ 2) + (x * (varA - varB - varB ^ 2)) - (varA * varB)
      derEq = (3 * x ^ 2) - (2 * x) + (varA - varB - varB ^ 2)
      nvalX = (x) - ((equation) / (derEq))
   Loop

   'once the do while loop is complete, the value obtained is stored in the first space in the array and so on
   compZ(i - 24) = nvalX
   'this was just to test the code to make sure it was working (it is)
   MsgBox compZ(i - 24)
Next

'how do I bring the values of this array into excel?

End Function

謝謝你

看起來你有一個一維數組。 應該很容易吧?

首先檢查數組的邊界,然后通過數組運行 for 循環以輸出到單元格。

Dim arrayLength as int
arrayLength = UBound(compZ) - LBound(compZ) + 1

For i = 1 To arrayLength      
       Cells(2,i).Value = compZ(i)  
Next i

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM