最小函数在VBA中工作不正常
我现在正在研究一个宏,它产生了奇怪的结果。具体不工作的部分是一个Min函数最小函数在VBA中工作不正常,vba,excel,excel-formula,Vba,Excel,Excel Formula,我现在正在研究一个宏,它产生了奇怪的结果。具体不工作的部分是一个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) - LinR
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())
在代码的底部,它将差值1和差值2存储在数组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
的值是否为预期值。您忽略了e和f数组的声明和尺寸标注。这是你问题的一个重要因素
当您将e和f声明为长数组或双数组时,它们被实例化为零值
Dim v() As Double, i As Long
ReDim v(5) '<~~ all zero values
For i = LBound(v) To UBound(v) - 1 '<~~fill all but the last one
v(i) = i + 10
Next i
Debug.Print Application.Min(v) 'zero as v(5) is zero
@QHarr一开始我有这个,然后把它改成现在的样子。我发现这没什么区别。如果眼睛没有(),我就去掉了(),但仍然得到了错误的结果。当我第一次使用e()来求最大值时,e()中的数字是正确的;但是,当我再次使用它来查找最小值时,e()中的数字不正确。您可能需要提供一些数据和预期输出。我在下面展示的语法对于查找1d数组的最小值很好。您检查过e的内容吗?我怀疑e和f的元素比diff1和diff2的元素多。这可以解释这样一个事实,即max可以工作,但min可能返回一个实例化但未赋值的零值。循环前的ReDim e(diff1)或循环后的ReDim Preserve(i-1)可以纠正此问题。@jeeped听起来很合乎逻辑。你应该把它贴出来作为答案。获取我的投票:-)您好,Jeeped,我在宏的前面确实有ReDim e(diff1)和ReDim f(diff2)。@Jeeped连接是一场噩梦。
Dim v() As Variant, i As Long
ReDim v(5) '<~~ all empty values
For i = LBound(v) To UBound(v) - 1 '<~~fill all but the last one
v(i) = i + 10
Next i
Debug.Print Application.Min(v) '10 as v(5) is empty and not considered in Min
'...
'redimension before the loop to the known ubound
redim e(diff1)
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
'...
'or redimension after the loop with Preserve
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
'i exits with a value 1 greater than diff2
redim preserve f(i-1)
'...