如何在Excel中的某一列用vba产生五组1到10的随机数?如何在Excel中的某一列产生五组1到10的随机数?例如:在A1至A50中产生五组1到10的随机数,产生一组容易实现,可是产生五组就有问题了,每一组
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/18 07:31:51
如何在Excel中的某一列用vba产生五组1到10的随机数?如何在Excel中的某一列产生五组1到10的随机数?例如:在A1至A50中产生五组1到10的随机数,产生一组容易实现,可是产生五组就有问题了,每一组
如何在Excel中的某一列用vba产生五组1到10的随机数?
如何在Excel中的某一列产生五组1到10的随机数?
例如:在A1至A50中产生五组1到10的随机数,产生一组容易实现,可是产生五组就有问题了,每一组产生的随机数要不一样,比如第一组产生的是2,8,10,3,7,5,4,1,9,6.第二组7,10,9,5,6,8,3,1,2,4等!
Public Sub sjs()
Dim rng As Range,rng1 As Range
Dim x As Integer,y As Integer,i As Integer
x = 1
y = 10
m = 50 / 10
For i = 1 To m
Set rng = Range("A(x):A(y)")
rng.ClearContents
Randomize
For Each rng1 In rng
Do
rng1 = Int(Rnd * 10 + 1)
Loop Until Application.WorksheetFunction.CountIf(rng,rng1) = 1
Next
x = x + 10
y = y + 10
Next
End Sub
如何在Excel中的某一列用vba产生五组1到10的随机数?如何在Excel中的某一列产生五组1到10的随机数?例如:在A1至A50中产生五组1到10的随机数,产生一组容易实现,可是产生五组就有问题了,每一组
能够产生一组随机数会了.五组如果不会用循环,就单独用5次就行了.如下:
Public Sub sjs()
'以下第一组
Dim a As Range, a1 As Range
Set a = Range("a1:a10")
a.ClearContents
Randomize
For Each a1 In a
Do
a1 = Int(1 + Rnd * 10)
Loop Until Application.WorksheetFunction.CountIf(a, a1) = 1
Next
'以下第二组
Dim b As Range, b1 As Range
Set b = Range("a11:a20")
b.ClearContents
Randomize
For Each b1 In b
Do
b1 = Int(1 + Rnd * 10)
Loop Until Application.WorksheetFunction.CountIf(b, b1) = 1
Next
'以下第三组
Dim c As Range, c1 As Range
Set c = Range("a21:a30")
c.ClearContents
Randomize
For Each c1 In c
Do
c1 = Int(1 + Rnd * 10)
Loop Until Application.WorksheetFunction.CountIf(c, c1) = 1
Next
'以下第四组
Dim d As Range, d1 As Range
Set d = Range("a31:a40")
d.ClearContents
Randomize
For Each d1 In d
Do
d1 = Int(1 + Rnd * 10)
Loop Until Application.WorksheetFunction.CountIf(d, d1) = 1
Next
'以下第五组
Dim e As Range, e1 As Range
Set e = Range("a41:a50")
e.ClearContents
Randomize
For Each e1 In e
Do
e1 = Int(1 + Rnd * 10)
Loop Until Application.WorksheetFunction.CountIf(e, e1) = 1
Next
End sub
说明:这是个土方法,但可用啊.