|
楼主 |
发表于 2019-1-11 14:04
|
显示全部楼层
本帖最后由 liuxi001 于 2019-1-12 23:24 编辑
修复一些BUG,代码调整优化,把学校代码、考室号、考号改为文本数字!座次排序搞好了,但是考号的编排却是个麻烦,考室不好确定。
- Sub 座次乱序()
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Dim ar, i
- Dim 班级列, 学校列, 姓名列, 总行, 总列
- 总列 = 11
- Sheet1.[a1].Resize(1, 总列) = Array("总序号", "座次序号", "考号", "考室", "学校代码", "班级", "姓名", "语文", "数学", "英语", "总分")
- For i = 1 To 总列
- '获取班级、考室和考号的列号
- If InStr(Sheet1.Cells(1, i), "班级") > 0 Then 班级列 = i
- If InStr(Sheet1.Cells(1, i), "学校") > 0 Then 学校列 = i
- If InStr(Sheet1.Cells(1, i), "姓名") > 0 Then 姓名列 = i
- If InStr(Sheet1.Cells(1, i), "座次序号") > 0 Then 座次列 = i
- Next
- 总行 = Sheet1.Cells.Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
- If 总行 = 1 Then MsgBox "《在校生》表中没有数据,请录入学校代码、班级和姓名后再试!": Exit Sub
- '获取总表数据
- ar = Sheet1.Range(Sheet1.[a2], Sheet1.Cells(总行, 总列))
- '判断是否已经乱序
- '如已经乱序,则按年级和班级从小到大的顺序排列之后再试
-
- Set 计数 = CreateObject("scripting.dictionary")
- For i = LBound(ar) To UBound(ar)
- s = ar(i, 学校列) & ar(i, 班级列)
- 计数(s) = 计数(s) + 1
- ar(i, 座次列) = ar(i, 学校列) & Left(ar(i, 班级列), 1) & Format(计数(s), "00") & Right(ar(i, 班级列), 2)
- Next
- '____________________________________________________________________
- '座次希尔排序
- Dim 总大小, 间隔, x, y, v, tmp(1 To 30)
- 总大小 = UBound(ar) - LBound(ar) + 1
- 间隔 = 1
- If 总大小 > 13 Then
- Do While 间隔 < 总大小
- 间隔 = 间隔 * 3 + 1
- Loop
- 间隔 = 间隔 \ 9
- End If
- Do While 间隔
- For x = LBound(ar) + 间隔 To UBound(ar)
- For v = 1 To 总列
- tmp(v) = ar(x, v)
- Next v
- For y = x - 间隔 To LBound(ar) Step -间隔
- '如果比tmp(座次列)小,则退出。按座次列排序
- If ar(y, 座次列) <= tmp(座次列) Then Exit For
- For v = 1 To 总列
- ar(y + 间隔, v) = ar(y, v)
- Next v
- Next y
- For v = 1 To 总列
- ar(y + 间隔, v) = tmp(v)
- Next v
- Next x
- 间隔 = 间隔 \ 3
- Loop
- Sheet1.Range("a2").Resize(总行, 总列) = ar
- End Sub
复制代码
|
|