如何在Excel中的某一列用vba产生五组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
答
能够产生一组随机数会了.五组如果不会用循环,就单独用5次就行了.如下:Public Sub sjs()'以下第一组Dim a As Range, a1 As RangeSet a = Range("a1:a10")a.ClearContentsRandomizeFor Each a1 In aDoa1 = Int(1 + Rnd...