[英]How to store results of for loop as an array in java? Is there any other way I can write this code neat way?
[英]how can I write to an array in a loop?
我對 VBA 比較陌生
我認為錯誤在於我的語法。
Bellow 是我正在研究的一個子程序。 數組“MyArray”始終為空。 我已經變暗和重新變暗,並根據我在網上看到的一百萬次更改了類型和語法,它總是 0 或為空。 請幫忙。
Sub CostPerTon()
Dim FeedType As String, count#, CostPerTon#
FeedType = "Duck - Comercial - Starter - 1 to 25 Days"
Sheets("Formulations").Activate
count = Application.WorksheetFunction.CountA(Range("A:A"))
i = 1
Dim IngredientCost As Double
Dim MyArray(100) As Variant
Do While i < count + 1
IngredientCost = Application.WorksheetFunction.VLookup(FeedType, Range("1:1048576"), i + 2, 0) _
* Worksheets("Costs").Cells(i + 2, 2) * (1 / 1000)
MyArray(i) = IngredientCost
i = i + 1
Loop
End Sub
當我在循環中循環時,變量“IngredientCost”有值,這樣函數就可以了。 它在 MyArray(i)=IngredientCost 行分解。 它只是保持空白
並且計數是14
問題是,您希望在 VLookup 函數中返回哪一列:
IngredientCost = Application.WorksheetFunction.VLookup(FeedType, Range("1:1048576"), i + 2, 0) _
* Worksheets("Costs").Cells(i + 2, 2) * (1 / 1000)
您定義的最后一個參數函數:
i + 2
因此,如果您發現“Duck - Comercial - Starter - 1 to 25 Days”,例如在 25. 行:--> i=25 並且 Vlookup 想要返回第 27 列,該列可能是空的。
考慮一下:
IngredientCost = Application.WorksheetFunction.VLookup(FeedType, Range("1:1048576"),3, 0) _
* Worksheets("Costs").Cells(i + 2, 2) * (1 / 1000)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.