|
楼主 |
发表于 2020-2-21 20:04
|
显示全部楼层
谢谢两位老师的答复,经测试,程序运行较快,不卡机,运用了字典方法,正是我想学的,但结果同行有重复数字,不太符合要求。能不能帮忙完善一下。
我原来编制过一个,没用字典方法,运行非常卡,连续几次就得重启excel,但能达到我要求,或者在我的思路上,给改进一下。
我的代码如下:垦请不吝赐教!!!
Sub 小区设计()
m = Range("b1") 'b1为重复数,3或4
n = Range("b2") 'b2为小区数,一般4-16之间
Range("3:" & Rows.Count).Clear
Range("c3:f18").Font.Size = 14
For i = 1 To n
iii:
Randomize
x = Int(Rnd * n) + 1
If Application.CountIf(Range("C:C"), x) = 0 Then
Cells(i + 2, 3) = x
Else
GoTo iii
End If
Next i
For j = 1 To n
jjj:
Randomize
y = Int(Rnd * n) + 1
If Application.CountIf(Range("D:D"), y) = 0 And Application.CountIf(Rows(j + 2), y) = 0 Then
Cells(j + 2, 4) = y
Else
GoTo jjj
End If
Next j
For k = 1 To n
kkk:
Randomize
Z = Int(Rnd * n) + 1
If Application.CountIf(Range("E:E"), Z) = 0 And Application.CountIf(Rows(k + 2), Z) = 0 Then
Cells(k + 2, 5) = Z
Else
GoTo kkk
End If
Next k
If m = 4 Then
For q = 1 To n
qqq:
Randomize
p = Int(Rnd * n) + 1
If Application.CountIf(Range("F:F"), p) = 0 And Application.CountIf(Rows(q + 2), p) = 0 Then
Cells(q + 2, 6) = p
Else
GoTo qqq
End If
Next q
End If
End Sub
|
|