写一段VBA代码,生成随机表14行,14列。每行为1到14的数字,每个数字每行只能出现一次。每列为1到14的数字,每个数字每行列只能出现一次。
时间: 2023-03-10 10:43:01 浏览: 98
VBA生成不重复的随机数源码.txt
5星 · 资源好评率100%
Sub RandomTable()
Dim i As Integer, j As Integer
Dim arr As Variant
arr = Range("A1:N14")
For i = 1 To 14
For j = 1 To 14
Do
arr(i, j) = Int(14 * Rnd + 1)
Loop Until Application.CountIf(Range(Cells(i, j), Cells(i, j)), arr(i, j)) = 1 And _
Application.CountIf(Range(Cells(i, 1), Cells(i, 14)), arr(i, j)) = 1 And _
Application.CountIf(Range(Cells(1, j), Cells(14, j)), arr(i, j)) = 1
Next j
Next i
Range("A1:N14") = arr
End Sub
阅读全文