0

我想在数组中收集循环的结果。理想情况下,我会保留第一个循环的结果并将其与第二个循环的结果进行比较。如果第二个循环的结果更好,那么我想保留第二个,将其与第三个进行比较,依此类推。关于如何做到这一点的任何想法?这是我到目前为止的摘录:

For Each ccell In pcrange
If ccell.Cells.Value = 0 Then
    For Each agecell In agerange
    If agecell.Cells.Value < 0.5 And agecell.Offset(0, 5).Value > 1 Then
    tmpyr = agecell.Offset(0, 1)
    srchyr = ccell.Offset(0, -6)
    tmpval = agecell.Offset(0, 2)
    tmpdiff = Abs(tmpyr - srchyr)
    arrindx = arrindx + 1

    End If

    tmparray = Array(tmpdiff, tmpval) 'need redim to hold more than 2 values? why are subsequent elements empty?
    ReDim Preserve tmparray(2 * arrindx - 1)
   'minarr = IIf(tmparray(2) > tmparray(0), tmparray(1), tmparray(3)); want to extract corresponding tmpval from array onto sheet if tmpdiff is the min of the entire loop; change to store better result each time
    Next 
End If
Next
4

1 回答 1

0

定义你的tmparray应该是这样的:

tmparray(UBound(tmparray, 1)) = Array(tmpdiff, tmpval) 'add values to last place
ReDim Preserve tmparray(UBound(tmparry, 1) + 1) 'add another place to the array

如果放在以下位置之后,则应该找到新的最小值ReDim

If UBound(tmparray, 1) <= 2 then 
    minarr = tmpval
ElseIf tmparray(UBound(tmparray, 1) - 1, 0) < tmparray(UBound(tmparray, 1) - 2, 0) then 
    minarr = tmparray(UBound(tmparray, 1) - 1, 1)
end if
于 2013-04-07T12:00:53.463 回答