0
我在將WorksheetFunction.Average應用於VBA中的動態數組時遇到問題。數組的元素是範圍,我希望能夠在動態數組中的整個範圍內計算平均值。我希望這是有道理的。 (即,在規則的公式欄中=平均值(D14:F15,E17:G17))。我做了大量的研究,找不到我的問題的答案。如何將WorksheetFunction.Average應用於VBA中的動態數組
With sht2.Range("B:B") 'week ending column
Set begin_upc = .Find(upc, LookIn:=xlValues) 'find the first occurence of the upc
Set end_upc = .Find(upc, LookIn:=xlValues, SearchDirection:=xlPrevious) 'find the last occurence of the upc
upcRange = "A" & Range(begin_upc.Address).Row & ":" & "A" & Range(end_upc.Address).Row 'range for UPCs
With sht2.Range(upcRange)
For r = LBound(starts) To UBound(starts)
Set begin_date = .Find(starts(r), LookIn:=xlValues) 'starts is an array of start dates for the .find within the UPC range
Set end_date = .Find(stops(r), LookIn:=xlValues, SearchDirection:=xlPrevious) 'stops is an array for the .find within the UPC range
rangeToAvg = "O" & Range(begin_date.Address).Row & ":" & "O" & Range(end_date.Address).Row 'format the range
ReDim Preserve rangesForAvg(r) 'resize the array
rangesForAvg(r) = rangeToAvg 'append the range
Next r
avg_during = WorksheetFunction.Average(sht2.Range(rangesForAvg)) 'this is the line I need help with
當你通過代碼(F8),看你的範圍內構建步驟,它有你期望它具有價值? – Rodger
我不知道如何使用F8,但是當我添加行對於r = LBound(rangesForAvg)到UBound(rangesForAvg)\ n Debug.Print rangesForAvg(r)\ n下一步r在for循環之後,所有正確的值在陣列中。 –
當你在VBE中點擊view/locals來打開你的本地窗口......它顯示了每個變量的值,因爲它在代碼的這一步中是這樣的。然後將光標放在代碼的頂部,然後按F8開始逐行進行。 ...它就像debug.print,但不需要將它放入。您還可以將光標懸停在變量上,以查看它們當前的狀態,如果它們不是數組,範圍等。 – Rodger