随机数字而不重复数字

时间:2015-04-10 03:46:18

标签: vba excel-vba excel

我的最终结果是以随机顺序将A列中的名称输出到B列。

我一直在研究,但似乎无法找到我需要的东西。

到目前为止,我可以将数字随机化,但它仍然给我重复数字+标题(A1)。

我需要它跳过A1,因为这是列的标题\标题,从A2开始。

我假设一旦工作正常我将randomNumber添加到Worksheets("Master Sheet").Cells(randomNumber, "B").Value的随机名称......类似的东西......?

或者,如果有更好的方法,请告诉我。

Sub Meow()

Dim CountedRows As Integer
Dim x As Integer
Dim i As Integer
Dim PreviousCell As Integer
Dim randomNumber As Integer

i = 1
PreviousCell = 0

CountedRows = Worksheets("Master Sheet").Range("A" & Rows.Count).End(xlUp).Row

If CountedRows < 2 Then
 ' If its only the heading then quit and display a messagebox
   No_People_Error = MsgBox("No People entered or found, in column 'A' of Sheetname 'Master Sheet'", vbInformation, "Pavle Says No!")
   Exit Sub
End If


Do Until i = CountedRows
 randomNumber = Int((Rnd * (CountedRows - 1)) + 1) + 1

 If Not PreviousCell = randomNumber Then
    Debug.Print randomNumber
    i = i + 1
 End If

 PreviousCell = randomNumber
Loop

Debug.Print "EOF"

End Sub

2 个答案:

答案 0 :(得分:2)

这是一个快速入侵......

  Sub Meow()
    'On Error GoTo err_error
    Dim CountedRows As Integer
    Dim x As Integer
    Dim i As Integer
    Dim PreviousCell As Integer
    Dim randomNumber As Integer
    Dim nums() As Integer
    PreviousCell = 0

    CountedRows = Worksheets("Master Sheet").Range("A" & Rows.Count).End(xlUp).Row
    ReDim nums(CountedRows - 1)
    If CountedRows < 2 Then
     ' If its only the heading then quit and display a messagebox
       No_People_Error = MsgBox("No People entered or found, in column 'A' of Sheetname 'Master Sheet'", vbInformation, "Pavle Says No!")
       Exit Sub
    End If

    For i = 1 To CountedRows
    rand:
        randomNumber = randomNumbers(1, CountedRows, nums)
        nums(i - 1) = randomNumber
        Worksheets("Master Sheet").Range("B" & randomNumber) = Range("A" & i)
    Next i


    Exit Sub
    err_error:
    Debug.Print Err.Description
    End Sub

    Public Function randomNumbers(lb As Integer, ub As Integer, used As Variant) As Integer
    Dim r As Integer
    r = Int((ub - lb + 1) * Rnd + 1)
    For Each j In used
        If j = r Then
            r = randomNumbers(lb, ub, used)
        Else
            randomNumbers = r
        End If
    Next

    End Function

答案 1 :(得分:1)

我之前使用过两个系列管理过类似的东西。

使用原始数据填充一个集合,并将另一个集合留空。然后随机选择第一个集合中的索引,将该索引处的值添加到第二个集合中,并从原始集合中删除该值。将其设置为循环,直到第一个集合为空,第二个集合将从您的起始列表中填充一组随机排序的唯一值。

***编辑:我再次考虑过这个问题,你真的不需要第二次收藏。您可以从第一个集合中弹出一个随机值,并将其直接写入工作表,每次都会递增该行:

Sub Meow()

Dim lst As New Collection
Dim rndLst As New Collection
Dim startRow As Integer
Dim endRow As Integer
Dim No_People_Error As Integer

startRow = 2
endRow = Worksheets("Master Sheet").Cells(startRow, 1).End(xlDown).Row

If Cells(startRow, 1).Value = "" Then
 ' If its only the heading then quit and display a messagebox
   No_People_Error = MsgBox("No People entered or found, in column 'A' of Sheetname 'Master Sheet'", vbInformation, "Pavle Says No!")
   Exit Sub
End If

' Fill a collection with the original list
Dim i As Integer
For i = startRow To endRow
    lst.Add Cells(i, 1).Value
Next i


' Create a randomized list collection
' Use i as a row counter
Dim rowCounter As Integer
rowCounter = startRow

Dim index As Integer

Do While lst.Count > 0

    'Find a random index in the original collection
    index = Int((lst.Count - 1 + 1) * Rnd + 1)
    'Place the value in the worksheet
    Cells(rowCounter, 2).Value = lst(index)
    'Remove the value from the list
    lst.Remove (index)
    'Increment row counter
    rowCounter = rowCounter + 1

Loop

End Sub

P.S。我希望在命名你的子Meow():P

背后有一个很好的故事