我有下面的代碼,應該找到範圍中的第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中做到這一點? –