一列中的五个最大值
我的目标是消息框中的五个最大的值。 但是因为我使用“>”L之前的列中的值不计算La或Lb。 例如:
7 8 5 3 6 2
L变成8
,但是La(第二大)变成6
(但应该是7
),Lb(第三大)变成2
,并且Lc = 0
,Ld = 0
。
Sub maxtest3() Dim L As Integer, La As Integer, Lb As Integer, Lc As Integer, Ld As Integer Dim a As Variant L = 0 La = 0 Lb = 0 Lc = 0 Ld = 0 For Each a In Range("A1:A20") If a.Value > L Then L = a.Value Else If a.Value > La Then La = a.Value Else If a.Value > Lb Then Lb = a.Value Else If a.Value > Lc Then Lc = a.Value Else If a.Value > Ld Then Ld = a.Value Else End If End If End If End If End If Next MsgBox (L & " " & La & " " & Lb & " " & Lc & " " & Ld) End Sub
我知道谁看到这个有能力用一行代码来解决问题,但是为了教育一个新手请不要这样做。
您嵌套的If ... Else ...
语句可以更容易地编写为Select Case语句 。 它甚至可能会提高可读性。
问题是随后的值覆盖了以前的值。 现有值需要在队列被覆盖之前进一步推入队列中。 虽然下面的代码有点冗长,但应该充分说明解决scheme。
Sub maxtest3() Dim L As Variant, a As Range, rng As Range Set rng = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row) ReDim L(1 To 5) For Each a In rng Select Case a.Value Case Is > L(1) L(5) = L(4) L(4) = L(3) L(3) = L(2) L(2) = L(1) L(1) = a.Value Case Is > L(2) L(5) = L(4) L(4) = L(3) L(3) = L(2) L(2) = a.Value Case Is > L(3) L(5) = L(4) L(4) = L(3) L(3) = a.Value Case Is > L(4) L(5) = L(4) L(4) = a.Value Case Is > L(5) L(5) = a.Value End Select Next a MsgBox Join(L, Chr(32)) End Sub
我已经将你的L 个variables改为一个简单的一维数组。 这允许使用连接函数来简化string连接到MsgBox
。
Sub test() Range("A1", "A6").Copy 'adjust the column you want to sort Range("B1", "B6").PasteSpecial 'adjust to a free column Columns("B").Sort key1:=Range("B1"), _ 'adjust order1:=xlDescending, Header:=xlNo Dim i As Integer Dim str As String For i = 1 To 5 str = str & " " & Cells(i, 2).Value 'adjust to the column you pasted too Next Range("B1", "B6").Clear 'adjust MsgBox str End Sub
Jeeped已经解释了为什么你的If Block
尝试失败的逻辑。
另一方面,因为您正在使用Excel,您可以利用可用的function 。
就像是:
Sub maxtest3() Dim L As Integer, La As Integer, Lb As Integer, Lc As Integer, Ld As Integer With Application.WorksheetFunction L = .Large(Range("A1:A20"), 1) La = .Large(Range("A1:A20"), 2) Lb = .Large(Range("A1:A20"), 3) Lc = .Large(Range("A1:A20"), 4) Ld = .Large(Range("A1:A20"), 5) End With MsgBox (L & " " & La & " " & Lb & " " & Lc & " " & Ld) End Sub
我希望这有帮助。
Sub maxtest3() Dim L(4) As Integer Dim Lp(4) As Integer Dim i, j As Integer Dim RC As Boolean Dim a As Variant For i = 0 To 4 L(i) = 0 For Each a In Range("A1:A20") 'If the current cell location has been used before sets RC = False RC = True For j = 0 To i - 1 If Lp(j) = a.Row Then RC = False Next j 'If the current cell value is greater than the current value in L(i) 'AND the location has not been used already, sets L(i) to the current 'value and sets Lp(i) to the location of the current value If a.Value > L(i) And RC = True Then L(i) = a.Value Lp(i) = a.Row End If Next Next i MsgBox (L(0) & " " & L(1) & " " & L(2) & " " & L(3) & " " & L(4))
结束小组
这个程序在五个不同的时间循环select范围。 每次循环时都会select下一个最大的数字。 L(i)和Lp(i)variables表示五个元素的数组,每个元素将在范围select中保存您的五个最大数字及其位置。 这样程序就可以跳过已经select的较大的数字。
需要考虑的事项:如果相同的数字在值的范围内出现多次,则可以将每个实例选为“最大”值。
正如目前所写,这个程序只考虑正整数。 如果范围内的所有数字都是负数,它将返回全零。 要解决这个问题,请将L(i)的初始化值从0更改为-32768(可以存储在整数中的最大负值)