- 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("a1:e" & r)
- End With
- For i = 2 To UBound(arr)
- If Not d.exists(arr(i, 2)) Then
- Set d(arr(i, 2)) = CreateObject("scripting.dictionary")
- End If
- For j = 3 To 5
- If Not d(arr(i, 2)).exists(arr(1, j)) Then
- m = 1
- ReDim brr(1 To 3, 1 To m)
- Else
- brr = d(arr(i, 2))(arr(1, j))
- m = UBound(brr, 2) + 1
- ReDim Preserve brr(1 To 3, 1 To m)
- End If
- brr(1, m) = m
- brr(2, m) = arr(i, 1)
- brr(3, m) = arr(i, j)
- d(arr(i, 2))(arr(1, j)) = brr
- Next
- Next
- For Each aa In d.keys
- For Each bb In d(aa).keys
- arr = d(aa)(bb)
- ReDim brr(1 To UBound(arr, 2), 1 To UBound(arr))
- For i = 1 To UBound(arr)
- For j = 1 To UBound(arr, 2)
- brr(j, i) = arr(i, j)
- Next
- Next
- On Error Resume Next
- Set ws = Worksheets(aa & "班" & bb)
- If Err Then
- Set ws = Worksheets.Add(after:=Worksheets(Worksheets.Count))
- ws.Name = aa & "班" & bb
- End If
- On Error GoTo 0
- With Worksheets(aa & "班" & bb)
- .Cells.Clear
- With .Range("a1")
- .Value = "成绩表"
- .Resize(1, IIf(bb <> "英语", 9, 6)).Merge
- With .Font
- .Size = 18
- .Name = "黑体"
- End With
- End With
- .Range("a2") = "学校"
- .Range("b2") = "胜利小学"
- If bb <> "英语" Then
- .Range("e2") = bb & "科"
- .Range("h2") = aa & "班"
- Else
- .Range("d2") = bb & "科"
- .Range("f2") = aa & "班"
- End If
- For j = 1 To IIf(bb <> "英语", 3, 2)
- .Cells(3, j * 3 - 2).Resize(1, 3) = Array("座号", "姓名", bb)
- Next
- .Range("a4").Resize(UBound(brr), UBound(brr, 2)) = brr
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- n = 4
- For i = IIf(bb <> "英语", 29, 44) To r Step IIf(bb <> "英语", 25, 40)
- .Cells(i, 1).Resize(IIf(bb <> "英语", 25, 40), 3).Cut Destination:=.Cells(4, n)
- n = n + 3
- Next
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- c = .Cells(3, .Columns.Count).End(xlToLeft).Column
- With .Range("a3").Resize(r - 2, c)
- .Borders.LineStyle = xlContinuous
- .BorderAround LineStyle:=xlContinuous, Weight:=xlMedium
- End With
- For j = 4 To c Step 3
- With .Cells(3, j).Resize(r - 2, 1).Borders(xlEdgeLeft)
- .LineStyle = xlContinuous
- .Weight = xlMedium
- End With
- Next
- With .UsedRange
- .HorizontalAlignment = xlCenter
- .VerticalAlignment = xlCenter
- End With
- End With
- Next
- Next
- End Sub
复制代码 |