|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub 统计人数()
- Dim cnn As New ADODB.Connection
- Dim rs As New ADODB.Recordset
- Dim sql As String
- Dim mybook As String
- mybook = ThisWorkbook.FullName
- With cnn
- .Provider = "microsoft.jet.oledb.4.0"
- .ConnectionString = "extended properties=""excel 8.0;HDR=YES;"";data source=" & mybook
- .Open
- End With
- sql = "select 班别,count(班别) from [考生$] group by 班别 order by 班别"
- rs.Open sql, cnn, adOpenKeyset, adLockOptimistic
- With Worksheets("各考场人数设定")
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- .Range("a3:b" & r).ClearContents
- .Range("a3").CopyFromRecordset rs
- r = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
- .Cells(r, 1) = "合计"
- .Cells(r, 2).FormulaR1C1 = "=SUM(R[" & 3 - r & "]C:R[-1]C)"
- End With
- End Sub
- Sub 排列座位()
- Dim r%, i%
- Dim arr, brr()
- Randomize Timer
- With Worksheets("考生")
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- arr = .Range("a2:e" & r)
- End With
- ReDim brr(1 To UBound(arr), 1 To 5)
- For i = 1 To UBound(arr)
- arr(i, 4) = Rnd()
- arr(i, 5) = i
- Next
- With Worksheets("座位排列")
- .Range("a2").Resize(UBound(arr), UBound(arr, 2)) = arr
- .Range("a2:e" & r).Sort Key1:=.Range("A2"), Order1:=xlAscending, Key2:=.Range("D2"), Order2:=xlAscending, Header:=xlNo
- arr = .Range("a2:c" & r)
- .Cells.Delete
- End With
- h = UBound(arr)
- n = h \ 2
- For i = 1 To n Step 2
- For j = 1 To 3
- brr(i, j) = arr(i, j)
- brr(i + 1, j) = arr(i + n, j)
- Next
- Next
- If h Mod 2 <> 0 Then
- For j = 1 To 3
- brr(i, j) = arr(i, j)
- Next
- End If
- With Worksheets("各考场人数设定")
- r = .Cells(.Rows.Count, "f").End(xlUp).Row
- crr = .Range("f3:g" & r)
- End With
-
- m = 0
- For i = 1 To UBound(crr)
- For j = 1 To crr(i, 2)
- m = m + 1
- If m > h Then Exit Sub
- brr(m, 3) = m
- brr(m, 4) = crr(i, 1)
- Next
- Next
- With Worksheets("座位排列")
- .Range("a1").Resize(1, 4) = Array("班别", "姓名", "考号", "座位号", "考场")
- .Range("a2").Resize(UBound(brr), UBound(brr, 2)) = brr
- End With
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|