|
本帖最后由 aoe1981 于 2014-7-24 15:34 编辑
hlly888 发表于 2014-7-23 23:11
分班前 已把 原始数据表按“总分”降序排序。
2380人 分班
以下是没有注释的完整代码(使用了香咱方法优化的代码,补加了打印信息沟通开关语句):
- Option Explicit
- Public Sub fengban()
- Dim n%, xx(), i&, bj%, m1&, m0&, h&, l&, tj(1 To 30, 1 To 3), t!, xbl%, zfl%, bjl%, nj$, xuex$, bjxx, gd, j&, k&, avgh%, j1%, i1%, lk
- t = Timer
- If 分班信息.[i4] = "" Then MsgBox "请先选择班级数!": Exit Sub
- Application.DisplayAlerts = False
- Application.ScreenUpdating = False
- Application.PrintCommunication = False
- lk = Array(2.75, 4.25, 8, 2.75, 4.25, 19, 8, 8, 16, 16, 11.5, 8.5, 4.25, 4.25, 4.25, 4.25, 4.25, 4.25)
- With 分班信息
- n = .[i4].Value '班级数
- nj = .[i3].Value '年级
- xuex = .[i2].Value '学校
- End With
- With 报名信息表
- xx = .UsedRange '信息
- xbl = .Rows(2).Find(what:="性别").Column
- bjl = .Rows(2).Find(what:="班级").Column
- zfl = .Rows(2).Find(what:="总分").Column
- End With
- h = UBound(xx) '报名信息表行数
- l = UBound(xx, 2) '报名信息表列数
- m1 = 0: m0 = n / 2 '置换参数(男、女)
- avgh = (h - 2) / n + 1 '分班后的平均行数
- ReDim bjxx(1 To n)
- ReDim gd(1 To avgh, 1 To l)
- For i = 1 To n
- bjxx(i) = gd
- Next i
- For i = Sheets.Count To 3 Step -1 '删除表
- Sheets(i).Delete
- Next i
- For i = 3 To h
- If xx(i, xbl) = "男" Then
- j = j + 1: bj = (j + m1) Mod n: If bj = 0 Then bj = n
- tj(bj, 1) = tj(bj, 1) + 1 '统计班级男生人数
- If j Mod n = 0 Then m1 = m1 + 1
- Else
- k = k + 1: bj = (k + m0) Mod n: If bj = 0 Then bj = n
- tj(bj, 2) = tj(bj, 2) + 1 '统计班级女生人数
- If k Mod n = 0 Then m0 = m0 + 1
- End If
- xx(i, bjl) = bj '填充班级信息
- tj(bj, 3) = tj(bj, 3) + xx(i, zfl) '统计班级总分
- For j1 = 1 To l
- bjxx(bj)(tj(bj, 1) + tj(bj, 2), j1) = xx(i, j1) '班级信息赋值
- Next j1
- Next i
- 分班信息.Range("c4:e33") = tj
- For i = 1 To n '表循环
- Sheets.Add after:=Sheets(Sheets.Count) '新建表
- With ActiveSheet
- .Range("f:f", "k:k").NumberFormatLocal = "@" '身份证号、电话为文本
- .Name = nj & i '工作表命名
- For k = 1 To l '列循环
- .Cells(2, k) = xx(2, k) '填充标题行
- Next k
- .[a3].Resize(avgh, l) = bjxx(i)
- With .UsedRange
- .Borders.LineStyle = xlContinuous '加边框
- .HorizontalAlignment = xlCenter '居中
- End With
- .Cells.EntireColumn.AutoFit '列自适应
- .[a1].Value = xuex & nj & "年级" & i & "班新生信息表" '添加标题并设置格式
- .[a1].Font.Size = 20
- .Range(.Cells(1, 1), .Cells(1, l)).HorizontalAlignment = xlCenterAcrossSelection '表头跨列居中
- For i1 = 0 To UBound(lk) '设置列宽
- .Columns(i1 + 1).ColumnWidth = lk(i1)
- Next i1
- .Rows(2).WrapText = True '标题行自动换行
- With .PageSetup '设置打印格式
- .PrintTitleRows = "$1:$2"
- .CenterFooter = "&N - &P"
- .LeftMargin = Application.InchesToPoints(0.236220472440945)
- .RightMargin = Application.InchesToPoints(0.236220472440945)
- .TopMargin = Application.InchesToPoints(0.748031496062992)
- .BottomMargin = Application.InchesToPoints(0.551181102362205)
- .HeaderMargin = Application.InchesToPoints(0.31496062992126)
- .FooterMargin = Application.InchesToPoints(0.31496062992126)
- .CenterHorizontally = True
- .Orientation = xlLandscape
- .PaperSize = xlPaperA4
- End With
- End With
- Next i
- Application.PrintCommunication = True
- Application.DisplayAlerts = True
- Application.ScreenUpdating = True
- 分班信息.Activate
- MsgBox Format(Timer - t, "0.0000")
- End Sub
复制代码 这个代码总长度与我的一样,共89+2=91行。 |
评分
-
1
查看全部评分
-
|