我在一列中有数字(即A列中的1至10),而另一列中有一些数字(即E列中有六个数字)。
我想将E列的数字随机放置在B列中,以使An和Bn之间的绝对差大于我想要的数字(D1)。
我使用了RandomSelection函数:
Function RandomSelection(aRng As Range)
Dim index As Integer
Randomize
index = Int(aRng.Count * Rnd + 1)
RandomSelection = aRng.Cells(index).Value
End Function
答案 0 :(得分:0)
您可以使用RANDBETWEEN
和ABS
公式来完成所需的工作。
Dim RndmzRng As Range
Set RndmzRng = Range("B2:B21")
Dim AbsValRng As Range
Set AbsValRng = Range("C2:C21")
Dim cel As range
With ActiveSheet
RndmzRng.Formula = "=RANDBETWEEN(1,6)" 'so you don't need data in ColE
'can be use with cell references, "=RANDBETWEEN($E$2,$E$3") where E2=1 and E3=6
AbsValRng.Formula = "=ABS(B2-A2)" 'Absolute formula
For Each cel In AbsValRng 'colors the cells green that are > the value in Range("D1")
If cel.Value > Range("D1").Value Then
cel.Value = cel.Value - Range("D1").Value
End If
Next
End With
答案 1 :(得分:0)