Excel VBA按顺序和数量生成随机数
G'day 我有一个棘手的问题,就是根据VBA代码或VBA中的公式所需的随机数,按排序顺序获取随机数。该需求在1到10之间随机生成 当它开始时看起来像这样 这是我所想到的效果,它根据示例中失败的数量显示排序的数字 这是我用VBA做的一次尝试,单元格J7包含我需要的随机数,但数字没有完全排序。我愿意接受这里的建议/反馈。非常感谢Excel VBA按顺序和数量生成随机数,excel,vba,random,formula,sorted,Excel,Vba,Random,Formula,Sorted,G'day 我有一个棘手的问题,就是根据VBA代码或VBA中的公式所需的随机数,按排序顺序获取随机数。该需求在1到10之间随机生成 当它开始时看起来像这样 这是我所想到的效果,它根据示例中失败的数量显示排序的数字 这是我用VBA做的一次尝试,单元格J7包含我需要的随机数,但数字没有完全排序。我愿意接受这里的建议/反馈。非常感谢 Public Const BeCoolMachineCounter As String = "J7" Public Const BeCoolMachineRange
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
Public Const BeCoolMachineCounter为String=“J7”
Public Const becoolmachinerage As String=“Q03:Q12”
'根据需要的数量生成随机数据。
调用CreateNumbers(范围(BeCoolMachineRange),范围(BeCoolMachineCounter).Value)
Private Sub CreateNumber(作为范围,作为整数的数量)
'变量声明
调光范围
长的
iCheck=1
'根据每个供应商所需的故障数量生成随机故障
对于其中的每个c
如果iCheck我不确定我是否理解你所说的,但基于之前和之后的假设,我假设你已经在列中有10个数字,你想从中随机抽取多少个,然后确保所取的数字按顺序排序
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
公共子随机样本(数据10作为范围,多少作为整数)
'在数据旁边插入随机数
Data10.Cells(1,2).FormulaR1C1=“=RAND()”
Data10.Cells(1,2).自动填充目标:=范围(Data10.Cells(1,2),Data10.Cells(10,2))
'按随机数对数据进行排序
范围(Data10.Cells(1,1),Data10.Cells(10,2))。排序键1:=Data10.Cells(1,2),头:=xlNo
'删除随机数
范围(Data10.Cells(1,2),Data10.Cells(10,2)).ClearContents
“删除剩余的数字到多少
如果数量小于10,则
范围(Data10.Cells(多少个+1,1),Data10.Cells(10,1)).ClearContents
如果结束
“剩下的数字是多少
范围(Data10.Cells(1,1),Data10.Cells(HowMany,1))。排序键1:=Data10.Cells(1,1),头:=xlNo
端接头
您可以使用RandomSample Range(“B3:B12”)调用此函数,6您可以在目标范围中使用数组公式和返回数组的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
(由Excel添加的大括号)
当然,您可以简单地从宏调用此UDF,但IMHO使用数组公式可以改善用户体验,因为所有内容都是透明的,并且每次更改计数时都会刷新列表
注意:快速排序的实现是从这里开始的:如果电子表格布局是静态的,我会尝试
SMALL
工作表函数。嗨,Juri,我认为这里不需要SMALL工作表函数,因为这意味着一个数字。感谢您的建议。=SMALL($B$3:$B$12),ROW()-2从C3复制到示例的单元格C3:C12
,对所有输出数据进行排序。您好Morbo,是的,这是正确的,10个数字,按排序顺序排列。为了便于说明,我在静态图片中使用了10个数字作为小样本。我将看看这段代码的作用,看看它是否符合我的想法。谢谢。我尝试过这段代码,我相信排序不起作用,因为它会在填充后清除数据。正如我在评论中所述,它在Excel 2003中对我起作用。如果您想要3个数字,它将删除剩余的7个数字。谢谢您的建议。我会检查代码并让您知道。谢谢,说真的,它可以按照我的要求工作。这解决了我的问题,一旦我记得如何做的CSE公式。
{=GetRandomFailures(A1)}