|
全部代码已经做好了。
就现有的数据,在我的电脑上用时不超过0.5秒
区间表
因数据比较少,就按照10名一组计算
分数线表
全部代码
Option Explicit
Sub A()
Dim cnn As New cConnection
Dim rs As New cRecordset
Dim sql$, S, arr, I, J, m, myf As String, D1, D2, BRR, CRR, t, s1
Dim mytim
mytim = Timer
Application.ScreenUpdating = False
Set D1 = CreateObject("Scripting.Dictionary")
Set D2 = CreateObject("Scripting.Dictionary")
Sheets("成绩").Activate
arr = Range("A1").CurrentRegion
For I = UBound(arr, 2) To 3 Step -1
If arr(1, I) Like "*名次" Then
S = Replace(Cells(1, I).Address(0, 0), 1, "")
Columns(S).Delete
End If
Next
arr = Range("A1").CurrentRegion
CRR = Range("P2:R13")
m = UBound(arr)
myf = ThisWorkbook.Path & "\T.DB"
cnn.CreateNewDB
sql = "CREATE TABLE T(考号,班级,姓名,科目,成绩)"
cnn.Execute sql
cnn.BeginTrans
For I = 2 To UBound(arr)
For J = 4 To UBound(arr, 2) - 3
S = arr(I, 1) & ",'" & arr(I, 2) & "','" & arr(I, 3) & "','" & arr(1, J) & "'," & arr(I, J)
sql = "INSERT INTO T VALUES(" & S & ")"
cnn.Execute sql
Next
Next
cnn.CommitTrans
arr = Array("语文", "数学", "英语", "日语", "西语", "物理", "化学", "生物", "政治", "历史", "地理", "总分")
For I = 0 To UBound(arr)
J = I * 3 + 4
Range("a1:b" & m).Offset(0, J).Insert Shift:=xlToRight
sql = "SELECT RANK() OVER (PARTITION BY 班级 ORDER BY 班级,成绩 DESC) AS 班名次," _
& "RANK() OVER (ORDER BY 成绩 DESC) AS 校名次 FROM T WHERE 科目='" & arr(I) & "' ORDER BY 考号"
rs.OpenRecordset sql, cnn
Range("a2").Offset(0, J).CopyFromRecordset rs.GetADORsFromContent
Range("a1").Offset(0, J) = arr(I) & "_班名次"
Range("a1").Offset(0, J + 1) = arr(I) & "_校名次"
Next
sql = "CREATE TABLE 区间 (下限,上限,组)"
cnn.Execute sql
cnn.BeginTrans
For I = 0 To 9
sql = "INSERT INTO 区间 VALUES(" & I * 10 + 1 & "," & I * 10 + 10 & ",'" & I * 10 + 1 & "-" & I * 10 + 10 & "名')"
cnn.Execute sql
Next
sql = "CREATE TABLE 分数线 (科目, 普本线, 高分线)"
cnn.Execute sql
For I = 1 To UBound(CRR)
sql = "INSERT INTO 分数线 VALUES('" & CRR(I, 1) & "'," & CRR(I, 2) & "," & CRR(I, 3) & ")"
cnn.Execute sql
Next
cnn.CommitTrans
sql = "SELECT 班级,科目,组,COUNT(*) AS 人数 FROM ( SELECT A.*,B.组 FROM (" _
& "SELECT 班级,科目,成绩,RANK() OVER (PARTITION BY 班级,科目 ORDER BY 班级,科目,成绩 DESC) AS 班名次 FROM t ) A " _
& "LEFT JOIN 区间 B ON A.班名次 BETWEEN B.下限 AND B.上限) GROUP BY 班级,科目,组"
rs.OpenRecordset sql, cnn
BRR = rs.GetRows
For J = 0 To UBound(BRR, 2)
s1 = BRR(0, J) & BRR(1, J) & BRR(2, J)
D1(s1) = BRR(3, J)
Next
sql = "SELECT 班级,科目,SUM(普本人数) as 普本人数,SUM(高分人数) AS 高分人数 FROM (" _
& " SELECT 班级,T.科目 AS 科目,成绩,成绩>=普本线 AS 普本人数,普本线,成绩>=高分线 AS 高分人数,高分线 FROM T " _
& " LEFT JOIN 分数线 ON t.科目=分数线.科目) GROUP BY 班级,科目"
rs.OpenRecordset sql, cnn
BRR = rs.GetRows
For J = 0 To UBound(BRR, 2)
s1 = BRR(0, J) & BRR(1, J)
D2(s1) = BRR(2, J) & "|" & BRR(3, J)
Next
Set rs = Nothing
Set cnn = Nothing
Sheets("一分一段").Activate
For I = 3 To 73 Step 6
Cells(I, 3).Resize(5, 12) = ""
Next
arr = [a1].CurrentRegion
For I = 3 To UBound(arr)
If arr(I, 1) <> "合计" And arr(I, 1) <> "班级" Then
t = arr(I, 2)
If t <> "" Then
Else
arr(I, 2) = arr(I - 1, 2)
t = arr(I, 2)
End If
For J = 3 To UBound(arr, 2) - 1
If J <= 12 Then
S = arr(I, 1) & t & arr(2, J)
arr(I, J) = D1(S)
Else
S = arr(I, 1) & t
arr(I, J) = Split(D2(S), "|")(0)
arr(I, J + 1) = Split(D2(S), "|")(1)
End If
Next
End If
Next
[a1].Resize(UBound(arr), UBound(arr, 2)) = arr
For I = 3 To 73 Step 6
Cells(I + 4, 3).Resize(1, 12).FormulaR1C1 = "=SUM(R[-4]C:R[-1]C)"
Next
Set D1 = Nothing
Set D2 = Nothing
Application.ScreenUpdating = True
MsgBox Format(Timer - mytim, "0.00")
End Sub
123.rar
(66.24 KB, 下载次数: 11)
|
评分
-
2
查看全部评分
-
|