- Sub test()
- Dim r%, i%
- Dim arr, brr
- Dim d As Object
- Set d = CreateObject("scripting.dictionary")
- With Worksheets("原始表")
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- arr = .Range("a2:d" & r)
- End With
- For i = 1 To UBound(arr)
- If Not d.exists(arr(i, 3)) Then
- Set d(arr(i, 3)) = CreateObject("scripting.dictionary")
- End If
- If Not d(arr(i, 3)).exists(arr(i, 1)) Then
- m = 1
- ReDim brr(1 To 2, 1 To m)
- Else
- brr = d(arr(i, 3))(arr(i, 1))
- m = UBound(brr, 2) + 1
- ReDim Preserve brr(1 To 2, 1 To m)
- End If
- brr(1, m) = arr(i, 2)
- brr(2, m) = IIf(arr(i, 4) = "组长", 0, 1)
- d(arr(i, 3))(arr(i, 1)) = brr
- Next
- With Worksheets("目标表")
- .Cells.Clear
- r = 1
- For Each aa In d.keys
- r0 = r
- r = r + 2
- With .Cells(r, 1).Resize(1, 7)
- .Value = Array("班级", "人数", "组长", "1", "2", "3", "4")
- .Borders.LineStyle = xlContinuous
- End With
- r = r + 1
- .Cells(r, 1) = "合计"
- With .Cells(r, 1).Resize(1, 7)
- .Borders.LineStyle = xlContinuous
- End With
- r = r + 1
- rs = 0
- For q = Application.Min(d(aa).keys) To Application.Max(d(aa).keys)
- If d(aa).exists(q) Then
- bb = q
- brr = d(aa)(bb)
- rs = rs + UBound(brr, 2)
- .Cells(r, 1) = bb
- .Cells(r, 2) = UBound(brr, 2)
- For x = 1 To UBound(brr, 2) - 1
- p = x
- For y = x + 1 To UBound(brr, 2)
- If brr(2, p) > brr(2, y) Then
- p = y
- End If
- Next
- If p <> x Then
- For k = 1 To 2
- temp = brr(k, x)
- brr(k, x) = brr(k, p)
- brr(k, p) = temp
- Next
- End If
- Next
- If brr(2, 1) = 0 Then
- .Cells(r, 3) = brr(1, 1)
- End If
- hs = Application.Ceiling(UBound(brr, 2) / 4, 1)
- ReDim crr(1 To hs, 1 To 4)
- m = 1
- n = 1
- For j = 1 To UBound(brr, 2)
- crr(m, n) = brr(1, j)
- n = n + 1
- If n > 4 Then
- n = 1
- m = m + 1
- End If
- Next
- With .Cells(r, 4).Resize(UBound(crr), UBound(crr, 2))
- .Value = crr
- End With
- With .Cells(r, 1).Resize(UBound(crr), 7)
- .Borders.LineStyle = xlContinuous
- End With
- r = r + UBound(crr)
- End If
- Next
- With .Cells(r0, 1)
- .Value = "人员名单(标记列为" & aa & ")(共" & rs & "人)"
- .Resize(1, 7).Merge
- End With
- .Cells(r0 + 3, 2) = rs
- r = r + 2
- Next
- With .UsedRange
- .HorizontalAlignment = xlCenter
- .VerticalAlignment = xlCenter
- End With
- End With
- End Sub
复制代码 |