我正在研究一个 vba 函数来计算一个数据范围的最大值除以另一个数据。一切似乎都运行良好,但是在尝试对数组执行 Application.Max 时似乎存在问题,我通过循环解决了它。但是我无法回忆循环外的 Max 值...第一个 Debug.Print 正常打印 MaxV 值,但是第二个 [循环外] Debug.Print MaxV 没有任何结果。CalDS2 函数得到一个#VALUE!错误。
Function CalDS2(RangeD As Range, RangeS As Range, MaxMin As String) As Variant
Dim i As Integer
Dim ArrayDS() As Variant
Dim MaxV As Variant
Dim MinV As Variant
MaxV = 0
MinV = 1000000000
For i = 1 To RangeD.Columns.Count
ArrayDS(i) = Round(RangeD.Cells(1, i) / RangeS.Cells(1, i), 4)
If ArrayDS(i) >= MaxV Then
MaxV = ArrayDS(i)
Debug.Print MaxV
End If
Next i
Debug.Print MaxV
CalDS2 = MaxV
End Function
任何帮助将不胜感激,安迪
** * ***新代码* ** * *
Function CalDS2(RangeD As Range, RangeS As Range, MaxMin As String) As Variant
Dim i As Long
Dim ArrayD, ArrayS, ArrayDS() As Variant
If UCase(MaxMin) <> "MAXIMUM" And UCase(MaxMin) <> "MINIMUM" Then
MsgBox "The 3rd argument - MaxMin - must be either Maximun or Mininum"
CalDS2 = "Error"
Exit Function
End If
ReDim ArrayDS(1 To RangeD.Columns.Count)
ArrayD = RangeD.Value
ArrayS = RangeS.Value
For i = 1 To RangeD.Columns.Count
ArrayDS(i) = ArrayD(1, i) / ArrayS(1, i)
Debug.Print ArrayDS(i)
Next i
If UCase(MaxMin) = "MAXIMUM" Then
CalDS2 = WorksheetFunction.Max(ArrayDS)
Else
CalDS2 = WorksheetFunction.Min(ArrayDS)
End If
End Function