我现在正在研究一个宏,它产生了奇怪的结果。具体不起作用的部分是 Min 函数。
a1RowTemp1 = a1Row
For i = 0 To diff1
intercept = Application.WorksheetFunction.intercept(a(),c())
LinReg1 = (slope * Cells(a1RowTemp1, 1)) + intercept
difference1 = Worksheets("GF9").Cells(a1RowTemp1, 2) - LinReg1
e(i) = difference1
a1RowTemp1 = a1RowTemp1 + 1
Next i
a2RowTemp2 = a2Row
For i = 0 To diff2
intercept2 = Application.WorksheetFunction.intercept(b(), d())
LinReg2 = (slope2 * Cells(a2RowTemp2, 1)) + intercept2
difference2 = Worksheets("GF9").Cells(a2RowTemp2, 2) - LinReg2
f(i) = difference2
a2RowTemp2 = a2RowTemp2 + 1
Next i
Worksheets("Chart").Cells(currentRow, 12) = Application.Max(e())
Worksheets("Chart").Cells(currentRow, 13) = Application.Min(e())
Worksheets("Chart").Cells(currentRow, 25) = Application.Max(f())
Worksheets("Chart").Cells(currentRow, 26) = Application.Min(f())
在代码的底部,它将 Difference1 和 Difference2 值存储在数组 e() 和 f() 中。当我使用函数 max/min 时,宏仅输出 max 函数的正确值。我怀疑这与我错误地使用数组有关。
最佳答案
如果 e 是一维数组,您应该能够编写
Application.WorksheetFunction.Min(e)
示例:
Option Explicit
Public Sub TEST()
Dim e()
e = Array(3, 4, 2, 5)
MsgBox Application.WorksheetFunction.Min(e)
End Sub
如果您仍然得到错误的值,则需要使用 F8 单步执行,并检查循环中分配给 e
的值是否是预期的值。
关于vba - Min 函数在 VBA 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51367052/