|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub test()
- Dim d As Object, arr, i%, j%, rng As Range, crr, n%
- Dim c, c1, c2, brr(1 To 100000, 1 To 7), drr()
- Dim 总人数, 总分, 合格人数, 优秀人数, y
- On Error GoTo L
- arr = Sheet1.Range("A1").CurrentRegion
- Set d = CreateObject("scripting.dictionary")
- Set dic = CreateObject("scripting.dictionary")
- For i = 2 To UBound(arr)
- If Not d.exists(arr(i, 2)) Then
- Set d(arr(i, 2)) = CreateObject("scripting.dictionary")
- End If
- If Not d(arr(i, 2)).exists(arr(i, 1)) Then
- Set d(arr(i, 2))(arr(i, 1)) = CreateObject("scripting.dictionary")
- End If
- d(arr(i, 2))(arr(i, 1))(arr(i, 3)) = ""
- Next
- n = 1
- brr(n, 1) = "科目": brr(n, 2) = "姓名": brr(n, 3) = "班别"
- brr(n, 4) = "平均分": brr(n, 5) = "合格率": brr(n, 6) = "优秀率"
- brr(n, 7) = "平均分排名"
- For Each c In d.keys
- Set rng = Sheet3.Cells.Find(c, lookat:=xlPart)
- crr = rng.CurrentRegion
- 总人数 = 0: 总分 = 0
- 合格人数 = 0: 优秀人数 = 0
- For Each c1 In d(c).keys
- n = n + 1
- j = j + 1
- brr(n, 1) = c
- brr(n, 2) = c1
- brr(n, 3) = Join(d(c)(c1).keys, "、")
- For Each c2 In d(c)(c1).keys
- 总人数 = 总人数 + crr(c2 + 2, 2)
- 总分 = 总分 + crr(c2 + 2, 3)
- 合格人数 = 合格人数 + crr(c2 + 2, 5)
- 优秀人数 = 优秀人数 + crr(c2 + 2, 7)
- Next
- ReDim Preserve drr(j - 1)
- brr(n, 4) = Round(总分 / 总人数, 2)
- drr(j - 1) = brr(n, 4)
- brr(n, 5) = Round(合格人数 / 总人数, 4) * 100
- brr(n, 6) = Round(优秀人数 / 总人数, 4) * 100
- Next
- For y = n To n - j + 1 Step -1
- brr(y, 7) = PM(brr(y, 4), drr)
- Next
- j = 0
- n = n + 2
- brr(n, 1) = "科目": brr(n, 2) = "姓名": brr(n, 3) = "班别"
- brr(n, 4) = "平均分": brr(n, 5) = "合格率": brr(n, 6) = "优秀率"
- brr(n, 7) = "平均分排名"
- L: Next
- Sheet2.Cells.Clear
- Sheet2.Range("A1").Resize(n, 7) = brr
- Sheet2.Columns().AutoFit
- Sheet2.Cells.HorizontalAlignment = xlCenter
- Sheet2.Rows(n).Delete
- For Each rng In Sheet2.Range("A1:A" & n)
- If rng.Value <> "" Then rng.CurrentRegion.Borders.LineStyle = 1
- Next
- Sheet2.Select
- End Sub
- Function PM(ByVal x1 As Double, ByVal arr As Variant) As Integer
- Dim n%, x%
- For x = 0 To UBound(arr)
- If arr(x) >= x1 Then
- n = n + 1
- End If
- Next
- PM = n
- End Function
复制代码 |
|