|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub test()
Dim ar As Variant
Dim brr(), crr()
With Sheet1
r = .Cells(Rows.Count, 2).End(xlUp).Row
ar = .Range("a1:c" & r)
For i = 5 To UBound(ar)
For s = i + 1 To UBound(ar)
If ar(i, 3) < ar(s, 3) Then
For j = 1 To 3
k = ar(i, j)
ar(i, j) = ar(s, j)
ar(s, j) = k
Next j
End If
Next s
Next i
ReDim arr(1 To UBound(ar), 1 To UBound(ar, 2))
ReDim brr(1 To UBound(ar), 1 To UBound(ar, 2))
For i = UBound(ar) To 5 Step -1
sl = sl + ar(i, 3)
If sl < 5000 Then
n = n + 1
For j = 1 To 3
arr(n, j) = ar(i, j)
Next j
m = m + 1
brr(m, 1) = ar(i, 1)
brr(m, 2) = 0
brr(m, 3) = 0
Else
ys = sl - 5000
n = n + 1
xh = i
arr(n, 1) = ar(i, 1)
arr(n, 2) = (ar(i, 3) - ys) / ar(i, 1)
arr(n, 3) = ar(i, 3) - ys
Exit For
End If
Next i
For i = xh To 5 Step -1
m = m + 1
If i = xh Then
brr(m, 1) = ar(i, 1)
brr(m, 2) = ys / ar(i, 1)
brr(m, 3) = ys
Else
For j = 1 To 3
brr(m, j) = ar(i, j)
Next j
End If
Next i
.[f5].Resize(n, 3) = arr
.[j5].Resize(m, 3) = brr
End With
End Sub
|
|