- Sub test1()
- Dim ar, i As Long, j As Long, k As Long, p As Long
-
- ar = Range("W21:FM34").Value
-
- For i = 1 To UBound(ar)
- k = UBound(ar, 2)
- BubbleSort ar, i, i, 1, UBound(ar, 2), i
- For j = UBound(ar, 2) To 1 Step -1
- If ar(i, j) <= 0 Then k = k - 1 Else Exit For
- Next
- If k Then
- p = -Int(-k / 2)
- If k Mod 2 Then ar(i, 1) = ar(i, p) Else ar(i, 1) = (ar(i, p) + ar(i, p + 1)) / 2
- Else
- ar(i, 1) = ""
- End If
- Next
- Range("FP21").Resize(UBound(ar), 1) = ar
- End Sub
- Function BubbleSort(ar, u As Long, d As Long, l As Long, r As Long, p As Long)
- Dim i As Long, j As Long, x As Long, y As Long, Flag As Boolean, Swap As Double
- For j = l To r - 1
- Flag = True
- For x = l To r + l - 1 - j
- If ar(p, x) < ar(p, x + 1) Then
- Flag = False
- For y = u To d
- Swap = ar(y, x)
- ar(y, x) = ar(y, x + 1)
- ar(y, x + 1) = Swap
- Next
- End If
- Next
- If Flag = True Then Exit For
- Next
- End Function
复制代码 |