Arrays 如何将变量值舍入到数组中的下一个最小值?

Arrays 如何将变量值舍入到数组中的下一个最小值?,arrays,vba,excel,Arrays,Vba,Excel,我正在使用VBA在Excel中处理宏。我有一系列数字,如: 180190300390400430。。。 然后我有一个值为的变量,比如说,307或389或425。如果不使用一堆If语句,如何将此数字向下舍入到数组中包含的下一个最低数字 例如,我需要改变: 307->300 389 -> 300 425 -> 400 有什么方法比使用一长串逻辑语句更快呢?您可以这样做 Private Sub CommandButton20_Click() Dim aValues(1 To 5) As Integ

我正在使用VBA在Excel中处理宏。我有一系列数字,如:

180190300390400430。。。
然后我有一个值为的变量,比如说,
307
389
425
。如果不使用一堆
If
语句,如何将此数字向下舍入到数组中包含的下一个最低数字

例如,我需要改变:

307->300
389 -> 300
425 -> 400

有什么方法比使用一长串逻辑语句更快呢?

您可以这样做

Private Sub CommandButton20_Click()
Dim aValues(1 To 5) As Integer
Dim i As Integer

aValues(1) = 180
aValues(2) = 190
aValues(3) = 300
aValues(4) = 390
aValues(5) = 400

Dim iFindValue As Integer
iFindValue = 310

'First make sure we have a valid value to check for.
If aValues(1) > iFindValue Then
    MsgBox ("Lowest value in the array is greater than variable.")
    Exit Sub
End If

'Loop through the array
For i = 1 To UBound(aValues)
    'If we are at the end of the array, we haven't found it so set the value to the highest slot in the array.
    If i = UBound(aValues) Then
        iFindValue = aValues(i)
        Exit Sub
    End If
    'If it is in between the current slot value and the next one, set it to the current. Effectively rounding it down.
    If aValues(i) < iFindValue And aValues(i + 1) > iFindValue Then
        iFindValue = aValues(i)
        Exit Sub
    End If
Next i

End Sub
Private Sub CommandButton20_Click()
Dim aValues(1 To 5) As Integer
Dim i As Integer

aValues(1) = 180
aValues(2) = 190
aValues(3) = 300
aValues(4) = 390
aValues(5) = 400

Dim iFindValue As Integer
iFindValue = 310

Dim iTemp As Integer
Dim iDifference As Integer
iDifference = 32000

For i = 1 To UBound(aValues)

    'Find the difference
    iTemp = iFindValue - aValues(i)
    'If it is the smallest difference found so far, record it
    If iTemp > 0 And iTemp < iDifference Then
        iDifference = iTemp
    End If
Next i

'Make the adjustment
iFindValue = iFindValue - iDifference

End Sub
Private子命令按钮20_单击()
将数值(1到5)设置为整数
作为整数的Dim i
Avalue(1)=180
Avalue(2)=190
平均值(3)=300
Avalue(4)=390
Avalue(5)=400
将iFindValue设置为整数
iFindValue=310
'首先确保我们要检查的值有效。
如果aValues(1)>iFindValue,则
MsgBox(“数组中的最小值大于变量。”)
出口接头
如果结束
'在数组中循环
对于i=1至UBound(aValues)
'如果我们在数组的末尾,我们还没有找到它,所以将该值设置为数组中的最高插槽。
如果i=UBound(aValues),则
iFindValue=aValues(一)
出口接头
如果结束
'如果在当前插槽值和下一个插槽值之间,请将其设置为当前插槽值。有效地将其四舍五入。
如果aValues(i)iFindValue,则
iFindValue=aValues(一)
出口接头
如果结束
接下来我
端接头
您说过可以对数组进行排序,但如果出于某种原因不能排序,您可以这样做

Private Sub CommandButton20_Click()
Dim aValues(1 To 5) As Integer
Dim i As Integer

aValues(1) = 180
aValues(2) = 190
aValues(3) = 300
aValues(4) = 390
aValues(5) = 400

Dim iFindValue As Integer
iFindValue = 310

'First make sure we have a valid value to check for.
If aValues(1) > iFindValue Then
    MsgBox ("Lowest value in the array is greater than variable.")
    Exit Sub
End If

'Loop through the array
For i = 1 To UBound(aValues)
    'If we are at the end of the array, we haven't found it so set the value to the highest slot in the array.
    If i = UBound(aValues) Then
        iFindValue = aValues(i)
        Exit Sub
    End If
    'If it is in between the current slot value and the next one, set it to the current. Effectively rounding it down.
    If aValues(i) < iFindValue And aValues(i + 1) > iFindValue Then
        iFindValue = aValues(i)
        Exit Sub
    End If
Next i

End Sub
Private Sub CommandButton20_Click()
Dim aValues(1 To 5) As Integer
Dim i As Integer

aValues(1) = 180
aValues(2) = 190
aValues(3) = 300
aValues(4) = 390
aValues(5) = 400

Dim iFindValue As Integer
iFindValue = 310

Dim iTemp As Integer
Dim iDifference As Integer
iDifference = 32000

For i = 1 To UBound(aValues)

    'Find the difference
    iTemp = iFindValue - aValues(i)
    'If it is the smallest difference found so far, record it
    If iTemp > 0 And iTemp < iDifference Then
        iDifference = iTemp
    End If
Next i

'Make the adjustment
iFindValue = iFindValue - iDifference

End Sub
Private子命令按钮20_单击()
将数值(1到5)设置为整数
作为整数的Dim i
Avalue(1)=180
Avalue(2)=190
平均值(3)=300
Avalue(4)=390
Avalue(5)=400
将iFindValue设置为整数
iFindValue=310
Dim iTemp作为整数
作为整数的Dim微分
I差异=32000
对于i=1至UBound(aValues)
“找出区别
iTemp=iFindValue-aValues(一)
如果这是迄今为止发现的最小差异,请记录下来
如果iTemp>0且iTemp
如果数组已排序,则只需使用
HLOOKUP()
。如果为最后一个参数指定
True
,它将与最接近的上一个值匹配。例如:

Debug.Print WorksheetFunction.HLookup(307, Array(180, 190, 300, 390, 400, 430), 1, True)
输出:

300
 39 
 15 
 12 
False

您只需要防止数字小于最低数字(本例中为180)。因此,您可能希望以值
0

开始数组。假设数组已排序,您可以使用:

Function RoundTo(A As Variant, num As Long) As Variant
    'returns greatest number in A which is <= num
    'returns False if no such number exists
    'assumes A is a sorted array of numbers

    Dim i As Long
    On Error Resume Next
    i = Application.WorksheetFunction.Match(num, A, 1)
    If Err.Number > 0 Then
        RoundTo = False
        Exit Function
    End If
    On Error GoTo 0
    RoundTo = A(LBound(A) + i - 1)

End Function
输出:

300
 39 
 15 
 12 
False

当然,当数字太小而无法四舍五入到数字列表中的任何数字时,您可以调整它以返回除
False
以外的内容

数字数组是否已排序?数组只是手动定义的,因此可以按需排序。