|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub test()
- Dim d As New Dictionary
- Dim r%, i%
- Dim arr, brr()
- With Worksheets("sheet1")
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- r = 18
- arr = .Range("a2:c" & r)
- For i = 1 To UBound(arr)
- If Not d.Exists(arr(i, 1)) Then
- Set d(arr(i, 1)) = CreateObject("scripting.dictionary")
- End If
- If IsNumeric(arr(i, 3)) Then
- d(arr(i, 1))(arr(i, 2)) = d(arr(i, 1))(arr(i, 2)) + arr(i, 3)
- End If
- Next
- End With
- For Each aa In d.Keys
- hj = Application.Sum(d(aa).Items)
- If hj <> 0 Then
- For Each bb In d(aa).Keys
- If IsNumeric(d(aa)(bb)) Then
- d(aa)(bb) = d(aa)(bb) / hj
- End If
- Next
- End If
- kk = d(aa).Keys
- tt = d(aa).Items
- For i = 0 To UBound(tt) - 1
- p = i
- For j = i + 1 To UBound(tt)
- If tt(p) < tt(j) Then
- p = j
- End If
- Next
- If p <> i Then
- temp = tt(i): tt(i) = tt(p): tt(p) = temp
- temp = kk(i): kk(i) = kk(p): kk(p) = temp
- End If
- Next
- ss = ""
- For i = 0 To UBound(kk)
- ss = ss & "/" & kk(i) & Format(tt(i), "0%")
- Next
- d(aa) = Mid(ss, 2)
- Next
- ReDim brr(1 To d.Count, 1 To 2)
- m = 0
- For Each aa In d.Keys
- m = m + 1
- brr(m, 1) = aa
- brr(m, 2) = d(aa)
- Next
- With Worksheets("sheet1")
- .Range("f1").Resize(UBound(brr), 2) = brr
- End With
- End Sub
复制代码 |
|