公共函数中的变量范围到数组

时间:2018-03-06 15:37:48

标签: arrays vba excel-vba excel

以下是我正在处理的代码。我有一个30x29数组,我需要从1x30数组中减去然后从新创建的数组中的指定列(Mnth)创建一个新数组,最后确定该数组中的最小值。我试图通过公共函数来做到这一点,我选择范围,然后输入所需的列。任何帮助将不胜感激

Public Function Implied_Ask(Spreads As Range, Bullets As Range, Mnth As Integer)

Dim arr(1 To 30, 1 To 29) As Variant
Dim arr2(1 To 30) As Variant
Dim arr3(1 To 30, 1 To 29) As Variant
Dim arr4(1 To 30) As Variant

Dim i As Integer, j As Integer, k As Integer

arr = Spreads
arr2 = Bullets

For i = 1 To 30
    For j = 1 To 29
        arr3(i, j) = arr2(i) - arr(i, j)
    Next j
Next i

For k = 1 To 30
    arr4(k) = arr3(k, Mnth)
Next k

Implied_Ask = Application.Min(arr4)

End Function

1 个答案:

答案 0 :(得分:0)

您需要对范围使用.Value以将它们分配给数组。另外,让范围本身决定数组的大小。

试试这个:

Public Function Implied_Ask(Spreads As Range, Bullets As Range, Mnth As Integer)

Dim arr() As Variant
Dim arr2() As Variant
Dim arr3(1 To 30, 1 To 29) As Variant
Dim arr4(1 To 30) As Variant

Dim i As Integer, j As Integer, k As Integer

arr = Spreads.Value
arr2 = Bullets.Value

For i = 1 To 30
    For j = 1 To 29
        arr3(i, j) = arr2(i, j) - arr(i, j)
    Next j
Next i

For k = 1 To 30
    arr4(k) = arr3(k, Mnth)
Next k

Implied_Ask = Application.Min(arr4)

End Function