方案:我有一個函數可以對組件的權重進行上限處理。使用VBA將函數結果數組輸出到工作表中
問題:當我試圖輸出我的結果輸出數組到Excel工作表,我不斷收到(其中應用功能上的Excel單元格)的值錯誤
我已經嘗試過:我想答案在此Excel VBA function to print an array to the workbook
,但我仍然得到同樣的錯誤
問:我怎樣才能正確地做這個程序?我正在使用這個操作有問題的功能嗎?
規範的相關部分:(DIM件等基本操作被拆除,以保持這個問題的簡潔)
Function Capping(ISIN As Range, Sector As Range, Weights As Range, W1 As Double, W2 As Double) As Variant
le = UBound(ArISIN)
ReDim Preserve ArCap(le, 8, 1)
For c = 1 To le
ArCap(c, 1, 1) = ArISIN(c, 1)
ArCap(c, 2, 1) = ArSector(c, 1)
ArCap(c, 3, 1) = ArWeight(c, 1)
Next c
For c = 1 To le
s = 0
For i = 1 To le
If ArCap(c, 2, 1) = ArCap(i, 2, 1) Then
s = s + ArCap(i, 3, 1)
End If
Next i
ArCap(c, 4, 1) = s
Next c
For c = 1 To le
If ArCap(c, 4, 1) >= W2 Then
ArCap(c, 5, 1) = 0.03
ArCap(c, 6, 1) = 0.03/ArCap(c, 3, 1) * ArCap(c, 4, 1)
Else
ArCap(c, 5, 1) = "Not Capped"
End If
Next c
suc = 0
scap = 0
For c = 1 To le
If ArCap(c, 5, 1) <> "Not Capped" Then
scap = scap + ArCap(c, 6, 1)
Else
suc = suc + ArCap(c, 3, 1)
End If
Next c
For c = 1 To le
If ArCap(c, 5, 1) = 0.03 Then
ArCap(c, 7, 1) = ArCap(c, 6, 1)
Else
ArCap(c, 7, 1) = ArCap(c, 3, 1)/suc * (1 - s)
End If
Next c
For c = 1 To le
s = 0
For i = 1 To le
If ArCap(c, 2, 1) = ArCap(i, 2, 1) Then
s = s + ArCap(i, 7, 1)
End If
Next i
ArCap(c, 8, 1) = s
Next c
ReDim Preserve OutArray(le, 0)
For c = 1 To le
OutArray(c, 0) = ArCap(c, 7, 1)
Next c
'this is where I am having problems:
ActiveSheet.Range("A4:UC4").Value = WorksheetFunction.Transpose(OutArray)
由一個獨立的子調用它測試你的功能 - 這樣你就可以調試任何錯誤,而不是在工作表單元格中獲取值錯誤。 –