|
本帖最后由 全党 于 2023-2-21 22:56 编辑
- Sub 试试()
- Dim brr(), arr, arr1, i, j, k, ss, cs, y, 场次, 学科, rng As Range
- [c7:v50].ClearFormats
- Set dic = CreateObject("Scripting.Dictionary")
- dic.comparemode = 1
- arr = Range("y7").CurrentRegion
- For i = 1 To UBound(arr)
- For k = 1 To arr(i, 5)
- Key = arr(i, 1) & arr(i, 3) & k
- dic.Add Key, arr(i, 3) & arr(i, 1)
- Next k
- Next i
- cs = Range("c5").End(xlToRight).Column
- With WorksheetFunction
- 场次 = .Transpose(WorksheetFunction.Transpose(Range("c5:v5")))
- 学科 = .Transpose(WorksheetFunction.Transpose(Range("c6:v6")))
- End With
- For y = 1 To cs - 3 + 1
- arr1 = VBA.Filter(dic.keys, Trim(学科(y)), False)
- s = UBound(arr1)
- p = 场次(y)
- ReDim brr(1 To p)
- For j = 1 To p
- n = Int((s - 1 + 0) * Rnd + 0)
- t = arr1(n): arr1(n) = arr1(j - 1): arr1(j - 1) = t
- Next j
- For ss = o To p - 2
- brr(ss + 1) = dic(arr1(ss))
- dic.Remove (CStr(arr1(ss)))
- Next
- Range(Cells(7, y + 2), Cells(p + 6, y + 2)) = Excel.Application.Transpose(brr)
- Erase brr
- Next y
- Set dic = Nothing
- For i = 3 To Range("c5").End(xlToRight).Column Step 2
- r = Cells(7, i).End(xlDown).Row
- Set 区域 = Range(Cells(7, i), Cells(r, i))
- For Each rng In 区域
- If rng = rng.Offset(0, 1) Then
- rng.Interior.ColorIndex = 3
- End If
- Next
- Next
- With [c7:v33]
- .Borders.LineStyle = 1
- .Font.Name = "宋体"
- .Font.Size = 12
- .HorizontalAlignment = xlCenter
- .VerticalAlignment = xlCenter
- End With
- End Sub
复制代码
粗略弄了一下,每个科目的最后一场给你设了空白,用来手动调节,没考虑同一考场的两人为一人情况,应该也好处理用for each 隔列比对判断即可,r,如果有这种情况,已经图红。我也是才接触VBA,凑合着用吧。看看哪个大师给优化一下,见笑了。
|
|