Excel VBA按顺序依次生成一个随机数

天儿真好,

我有一个棘手的问题,根据我需要的VBA代码或VBA中的公式,以sorting顺序获得随机数字。 这个需求是在1到10之间随机产生的。

它看起来像这样,当它开始。

在这里输入图像说明

这里是我想到的效果,根据示例中的失败次数显示sorting的数字。

在这里输入图像说明

这是VBA的一个尝试,我做了单元格J7包含我需要的数量的随机数,但数字没有完全sorting。 我在这里接受build议/反馈。 非常感谢。

Public Const BeCoolMachineCounter As String = "J7" Public Const BeCoolMachineRange As String = "Q03:Q12" 'Generate the random data according to how many needed. Call CreateNumbers(Range(BeCoolMachineRange), Range(BeCoolMachineCounter).Value) Private Sub CreateNumbers(Which As Range, HowMany As Integer) ' Declaration of variables Dim c As Range Dim iCheck As Long iCheck = 1 ' Generate random failures based on the number of required for each supplier For Each c In Which If iCheck <= HowMany Then c.Value = Random1to2192 iCheck = iCheck + 1 End If Next c End Sub 

您可以在目标范围内使用数组公式,并使用UDF返回数组。

它给你的结果你显示。

所以,UDF:

 Public Function GetRandomFailures(count As Long) As Variant Dim result As Variant, numbers As Variant ReDim result(100) ReDim numbers(count - 1) For i = 0 To count - 1 numbers(i) = Application.WorksheetFunction.RandBetween(1, 10000) Next i Call QuickSort(numbers, LBound(numbers), UBound(numbers)) For i = 0 To 99 If i < count Then result(i) = numbers(i) Else result(i) = "" End If Next i GetRandomFailures = Application.WorksheetFunction.Transpose(result) End Function Public Sub QuickSort(vArray As Variant, inLow As Long, inHi As Long) Dim pivot As Variant Dim tmpSwap As Variant Dim tmpLow As Long Dim tmpHi As Long tmpLow = inLow tmpHi = inHi pivot = vArray((inLow + inHi) \ 2) While (tmpLow <= tmpHi) While (vArray(tmpLow) < pivot And tmpLow < inHi) tmpLow = tmpLow + 1 Wend While (pivot < vArray(tmpHi) And tmpHi > inLow) tmpHi = tmpHi - 1 Wend If (tmpLow <= tmpHi) Then tmpSwap = vArray(tmpLow) vArray(tmpLow) = vArray(tmpHi) vArray(tmpHi) = tmpSwap tmpLow = tmpLow + 1 tmpHi = tmpHi - 1 End If Wend If (inLow < tmpHi) Then QuickSort vArray, inLow, tmpHi If (tmpLow < inHi) Then QuickSort vArray, tmpLow, inHi End Sub 

和样本公式:

 {=GetRandomFailures(A1)} 

(由Excel添加大括号)

你当然可以简单地从一个macros调用这个UDF,但是使用数组公式可以改善用户体验,因为它们都是透明的,每次改变计数时都会刷新列表。

注意:快速sorting实现是从这里: VBA数组sorting函数?

我不确定我是否理解你所说的内容,但根据之前和之后的假设,你已经在列中有10个数字,你想从他们那里随机抽取一个大小为HowMany的样本,然后确认然后按照顺序对所采取的数字进行sorting。

 Public Sub RandomSample(Data10 As Range, HowMany As Integer) ' Insert random numbers next to the data Data10.Cells(1, 2).FormulaR1C1 = "=RAND()" Data10.Cells(1, 2).AutoFill Destination:=Range(Data10.Cells(1, 2), Data10.Cells(10, 2)) ' Sort the data by the random numbers Range(Data10.Cells(1, 1), Data10.Cells(10, 2)).Sort key1:=Data10.Cells(1, 2), header:=xlNo ' Remove the random numbers Range(Data10.Cells(1, 2), Data10.Cells(10, 2)).ClearContents ' Remove numbers surplus to HowMany If HowMany < 10 Then Range(Data10.Cells(HowMany + 1, 1), Data10.Cells(10, 1)).ClearContents End If ' Resort the remaining numbers Range(Data10.Cells(1, 1), Data10.Cells(HowMany, 1)).Sort key1:=Data10.Cells(1, 1), header:=xlNo End Sub 

你可以用RandomSample Range(“B3:B12”),6来调用它