|
突然想到一个不用增加循环,只要增加一个判断条件的办法,可以做到最大日期不在最后一行也能取到最大日期的办法了,
- Sub 取数()
- Application.ScreenUpdating = False
- Dim ar As Variant
- Dim d As Object
- Dim br()
- Set d = CreateObject("scripting.dictionary")
- With Sheet1
- r = .Cells(Rows.Count, 2).End(xlUp).Row
- ar = .Range("a1:e" & r)
- ReDim br(1 To 100000, 1 To 3)
- For i = 2 To UBound(ar)
- If Trim(ar(i, 2)) <> "" Then
- If InStr(ar(i, 3), ",") = 0 Then
- zf = ar(i, 2) & ar(i, 3)
- t = d(zf)
- If t = "" Then
- k = k + 1
- d(zf) = k
- t = k
- br(k, 1) = zf
- br(k, 2) = ar(i, 4)
- End If
- br(t, 3) = br(t, 3) + ar(i, 5)
- If br(k, 2) < ar(i, 4) Then br(t, 2) = ar(i, 4)
- ElseIf InStr(ar(i, 3), ",") > 0 Then
- rr = Split(ar(i, 3), ",")
- sl = UBound(rr) + 1
- For s = 0 To UBound(rr)
- zf = ar(i, 2) & rr(s)
- t = d(zf)
- If t = "" Then
- k = k + 1
- d(zf) = k
- t = k
- br(k, 1) = zf
- br(k, 2) = ar(i, 4)
- End If
- If br(k, 2) < ar(i, 4) Then br(t, 2) = ar(i, 4)
- br(t, 3) = br(t, 3) + (ar(i, 5) / sl)
- Next s
- End If
- End If
- Next i
- rs = .Cells(Rows.Count, 7).End(xlUp).Row
- .Range("g2:i" & rs + 1).Borders.LineStyle = 0
- .Range("g2:i" & rs + 1) = Empty
- .[g2].Resize(k, UBound(br, 2)) = br
- .[g2].Resize(k, UBound(br, 2)).Borders.LineStyle = 1
- End With
- Application.ScreenUpdating = True
- MsgBox "ok!"
- End Sub
复制代码 |
|