|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub test()
- Dim d As New Dictionary
- Dim d1 As New Dictionary
- Dim r%, i%, c%, j%
- Dim arr, brr()
- With Worksheets("sheet1")
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- arr = .Range("a2:c" & r)
- End With
- For i = 1 To UBound(arr)
- arr(i, 3) = Rnd()
- Next
- For i = 1 To UBound(arr) - 1
- p = i
- For j = i + 1 To UBound(arr)
- If arr(p, 3) > arr(j, 3) Then
- p = j
- End If
- Next
- If p <> i Then
- For k = 1 To 3
- temp = arr(i, k)
- arr(i, k) = arr(p, k)
- arr(p, k) = temp
- Next
- End If
- Next
- ReDim brr(1 To 27, 1 To 9)
- r = 23
- c = 1
- m = 1
- n = 1
- For i = 1 To UBound(arr)
- brr(r, c) = "座位号:" & Format(n, "00")
- brr(r - 1, c) = arr(i, 1)
- brr(r - 2, c) = arr(i, 2)
- xm = arr(i, 1) & "+" & arr(i, 2)
- d1(xm) = m & "试室" & Format(n, "00号")
- n = n + 1
- If c = 1 Or c = 5 Or c = 9 Then
- r = r - 4
- Else
- r = r + 4
- End If
- If r = -1 Then
- c = c + 2
- r = 3
- ElseIf r = 27 Then
- c = c + 2
- r = 23
- End If
- If c > 9 Or i = UBound(arr) Then
- brr(26, 5) = "讲台"
- brr(26, 9) = "第" & m & "试室"
- brr(27, 9) = "共" & n - 1 & "人"
- d(m) = brr
- ReDim brr(1 To 27, 1 To 9)
- r = 23
- c = 1
- n = 1
- m = m + 1
- End If
- Next
- m = 1
- With Worksheets("sheet2")
- .Cells.Clear
- For Each aa In d.Keys
- brr = d(aa)
- .Cells(m, 1).Resize(UBound(brr), UBound(brr, 2)) = brr
- For i = 1 To 21 Step 4
- For j = 1 To 9 Step 2
- With .Cells(i + m - 1, j).Resize(3, 1)
- .HorizontalAlignment = xlCenter
- .VerticalAlignment = xlCenter
- .BorderAround LineStyle:=xlContinuous, Weight:=xlMedium
- End With
- Next
- Next
- With .Cells(m + 25, 5)
- .HorizontalAlignment = xlCenter
- .VerticalAlignment = xlCenter
- .BorderAround LineStyle:=xlContinuous, Weight:=xlMedium
- End With
- With .Cells(m + 25, 9).Resize(2, 1)
- .HorizontalAlignment = xlCenter
- .VerticalAlignment = xlCenter
- End With
- m = m + 31
- Next
- End With
- With Worksheets("sheet1")
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- arr = .Range("a2:c" & r)
- For i = 1 To UBound(arr)
- xm = arr(i, 1) & "+" & arr(i, 2)
- If d1.Exists(xm) Then
- arr(i, 3) = d1(xm)
- End If
- Next
- .Range("c1") = "试室号"
- .Range("c2").Resize(UBound(arr), 1) = Application.Index(arr, 0, 3)
- End With
- End Sub
复制代码 |
|