Sub test_dic()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim k As Long, Renshu As Long, tm
Dim D_1 As Object, D_2 As Object, D_3 As Object, D_4 As Object
Application.ScreenUpdating = False
tm = Timer
Renshu = 50
Set ws1 = ThisWorkbook.Worksheets("成绩")
Set ws2 = ThisWorkbook.Worksheets("统计")
Set D_1 = CreateObject("Scripting.Dictionary")
Set D_2 = CreateObject("Scripting.Dictionary")
Set D_3 = CreateObject("Scripting.Dictionary")
Set D_4 = CreateObject("Scripting.Dictionary")
'循环“成绩”表,得到不同的字典
For k = 2 To ws1.UsedRange.Rows.Count
'筛选有“第1学期期中考”项,并按班级合并“总分”
If ws1.Cells(k, 7).Value = "第1学期期中考" Then
D_1(ws1.Cells(k, 1).Value) = D_1(ws1.Cells(k, 1).Value) + ws1.Cells(k, 6).Value
End If
'筛选有“第1学期期末考”项,并按班级合并“总分”
If ws1.Cells(k, 7).Value = "第1学期期末考" Then
D_2(ws1.Cells(k, 1).Value) = D_2(ws1.Cells(k, 1).Value) + ws1.Cells(k, 6).Value
End If
'筛选有“第2学期期中考”项,并按班级合并“总分”
If ws1.Cells(k, 7).Value = "第2学期期中考" Then
D_3(ws1.Cells(k, 1).Value) = D_3(ws1.Cells(k, 1).Value) + ws1.Cells(k, 6).Value
End If
'筛选有“第2学期期末考”项,并按班级合并“总分”
If ws1.Cells(k, 7).Value = "第2学期期末考" Then
D_4(ws1.Cells(k, 1).Value) = D_4(ws1.Cells(k, 1).Value) + ws1.Cells(k, 6).Value
End If
Next k
'循环“统计”表,从字典取值填入表中
For k = 2 To ws2.UsedRange.Rows.Count
ws2.Cells(k, 2).Value = Round(D_1(ws2.Cells(k, 1).Value) / Renshu, 2)
ws2.Cells(k, 3).Value = Round(D_2(ws2.Cells(k, 1).Value) / Renshu, 2)
ws2.Cells(k, 4).Value = Round(D_3(ws2.Cells(k, 1).Value) / Renshu, 2)
ws2.Cells(k, 5).Value = Round(D_4(ws2.Cells(k, 1).Value) / Renshu, 2)
Next k
Debug.Print "arr用时:" & (Timer - tm)
Application.ScreenUpdating = True
End Sub