|
- Sub test2()
- Dim r%, i%
- Dim arr, brr
- Dim bjrs() As Integer
- Set d = CreateObject("Scripting.Dictionary")
- Set d1 = CreateObject("Scripting.Dictionary")
- Set d2 = CreateObject("Scripting.Dictionary")
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Randomize Timer
- With Worksheets("预分班信息")
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- If r > 1 Then
- arr = .Range("a2:b" & r)
- For i = 1 To UBound(arr)
- d2(arr(i, 1)) = arr(i, 2)
- Next
- End If
- End With
- With Worksheets("一年级分班花名册")
- .AutoFilterMode = False
- r = .Cells(.Rows.Count, 3).End(xlUp).Row
- arr = .Range("a1:o" & r)
- bjsl = .Range("q2").Value
- ReDim bjrs(1 To bjsl)
- For i = 7 To UBound(arr)
- If d2.exists(arr(i, 2)) Then
- bj = d2(arr(i, 2))
- bjrs(bj) = bjrs(bj) + 1
- If Not d.exists(bj) Then
- Set d(bj) = .Range("a5:o6")
- End If
- Set d(bj) = Union(d(bj), .Cells(i, 1).Resize(1, 15))
- Else
- If Not d1.exists(arr(i, 3)) Then
- Set d1(arr(i, 3)) = CreateObject("Scripting.Dictionary")
- End If
- d1(arr(i, 3))(i) = Empty
- End If
- Next
- For Each aa In Array("男", "女")
- If d1.exists(aa) Then
- kk = d1(aa).keys
- For i = 0 To UBound(kk) - 1
- n = Int(Rnd() * (UBound(kk) - i)) + i + 1
- temp = kk(i)
- kk(i) = kk(n)
- kk(n) = temp
- Next
-
- For i = 0 To UBound(kk)
- bj = 1
- For k = 2 To UBound(bjrs)
- If bjrs(k) < bjrs(bj) Then
- bj = k
- End If
- Next
- bjrs(bj) = bjrs(bj) + 1
- If Not d.exists(bj) Then
- Set d(bj) = .Range("a5:o6")
- End If
- Set d(bj) = Union(d(bj), .Cells(kk(i), 1).Resize(1, 15))
- Next
- End If
- Next
- End With
- For q = 1 To bjsl
- If d.exists(q) Then
- shtname = q & "班"
- On Error Resume Next
- Worksheets(shtname).Delete
- On Error GoTo 0
- Set ws = Worksheets.Add(after:=Worksheets(Worksheets.Count))
- With ws
- .Name = shtname
- With .Range("a1")
- .Value = "2024年秋季一年级" & q & "班花名册"
- .Resize(1, 15).Merge
- With .Font
- .Name = "微软雅黑"
- .Size = 20
- End With
- End With
- d(q).Copy .Range("a3")
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- arr = .Range("a5:c" & r)
- xy = 0
- xx = 0
- For i = 1 To UBound(arr)
- arr(i, 1) = i
- If arr(i, 3) = "男" Then
- xy = xy + 1
- Else
- xx = xx + 1
- End If
- Next
- .Range("a5:c" & r) = arr
- With .Range("a2")
- .Resize(1, 15).Merge
- .Value = Space(4) & "总人数:" & UBound(arr) & "人,其中男生:" & xy & "人,女生:" & xx & "人"
- With .Font
- .Name = "微软雅黑"
- .Size = 12
- End With
- End With
- With .UsedRange
- .HorizontalAlignment = xlCenter
- .VerticalAlignment = xlCenter
- End With
- .Rows(1).RowHeight = 30
- .Rows(2).RowHeight = 18
- .Rows(3).Resize(2).RowHeight = 30
- .Rows(5).Resize(UBound(arr)).RowHeight = 18
- End With
- End If
- Next
- Application.ScreenUpdating = True
- End Sub
复制代码 |
|