2013-01-05 23 views
2

天兒真好,Excel VBA中產生一個隨機數,以便根據有多少

我有按照排序順序獲得隨機數的棘手的問題我需要多少由內VBA無論是VBA代碼或公式。這需要在1到10之間隨機生成。

它在啓動時看起來像這樣。

enter image description here

,這裏是我腦子裏想的效果,即它是根據有多少的例子失敗排序號。

enter image description here

這是一個試圖通過VBA我在哪裏電池J7包含隨機的我需要多少,但數量不大排序。我願意在這裏提供建議/反饋。非常感謝。

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 
+0

如果電子表格佈局是靜態的,我想嘗試'SMALL'工作表函數。 –

+0

嗨朱裏,我不認爲小工作表函數是必需的,因爲這意味着一個數字。感謝您提出建議。 –

+0

'=小($ B $ 3:$ B $ 12),ROW() - 2'從C3複製到單元格'C3:C12'您的示例將排序所有輸出數據。 –

回答

2

你可以在目標範圍內使用一個數組式和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添加)

當然你也可以簡單地調用從宏觀此UDF但恕我直言,使用數組公式可以改善用戶體驗,因爲它們都是透明的,每次更改計數時都會刷新列表。

注:快速排序的實現就是從這裏開始:VBA array sort function?

+0

感謝您的建議。我會審查代碼並通知您。 –

+0

非常感謝,它按我的意願工作。一旦我想起如何完成CSE公式,這解決了我的問題。 –

1

我不知道我理解你所說的,而是基於之前,我已經假設後,你已經在列的10個數字,你想獲得大小的隨機樣本HowMany從他們,然後確保所採取的數字,然後按順序排序。

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範圍稱之爲( 「B3:B12」),6

+0

嗨Morbo,是的,這是正確的,10個數字和排序順序。爲了便於說明,我在作爲小樣本提供的靜態圖片中使用了10個數字。我會看看這段代碼的作用,看看這是否符合我的想法。謝謝。 –

+0

我用這段代碼去了,我認爲排序不起作用,因爲它一旦填充就清除數據。 –

+0

正如我在評論中所描述的那樣,它在Excel 2003中爲我工作。如果你想要3個號碼,它會刪除剩下的7個號碼。 – Morbo