我有下面的代码,应该找到范围中的第1,2,3个和第4个最高值。Excel VBA - 查找范围内的最高值和后续值
它目前是非常基本的,我有它提供了一个MsgBox的值,所以我可以确认它正在工作。
但是,它只找到最高值和第二高值。第三个和第四个值返回为0.我错过了什么?
Sub Macro1()
Dim rng As Range, cell As Range
Dim firstVal As Double, secondVal As Double, thirdVal As Double, fourthVal As Double
Set rng = [C4:C16]
For Each cell In rng
If cell.Value > firstVal Then firstVal = cell.Value
If cell.Value > secondVal And cell.Value < firstVal Then secondVal =
cell.Value
If cell.Value > thirdVal And cell.Value < secondVal Then thirdVal =
cell.Value
If cell.Value > fourthVal And cell.Value < thirdVal Then fourthVal =
cell.Value
Next cell
MsgBox "First Highest Value is " & firstVal
MsgBox "Second Highest Value is " & secondVal
MsgBox "Third Highest Value is " & thirdVal
MsgBox "Fourth Highest Value is " & fourthVal
End Sub
另一种方法将排序范围,然后拿起你的价值:) –
你真的需要在VBA中做到这一点? –