Randomize values in a collection

Posted on 2009-12-31
Medium Priority
Last Modified: 2012-05-08
Hello, Does anyone know of a quick way to randomize the values in a collection?  I have a collection of 400 cell addresses (A1 through U2) that I need to randomize.

Question by:JamesCbury
LVL 81

Accepted Solution

zorvek (Kevin Jones) earned 1000 total points
ID: 26156146
Public Sub RandomizeCollection()

    Dim SortedValues As Collection
    Dim RandomValues As Collection
    Dim Index As Long
    Set SortedValues = New Collection
    For Index = 1 To 10
        SortedValues.Add Index
    Next Index
    Set RandomValues = New Collection
    For Index = 1 To SortedValues.Count
        AddValueToRandomCollection SortedValues.Item(Index), RandomValues
    Next Index

End Sub

Public Sub AddValueToRandomCollection( _
      ByVal Value As Variant, _
      ByVal Values As Collection _

' Add the parameter Value to the collection in a random position.

   Dim Index As Long

   Index = Rnd * (Values.Count + 1) + 1
   If Index > Values.Count Then
      Values.Add Value
      Values.Add Value, Before:=Index
   End If

End Sub


Author Closing Comment

ID: 31671687
Very cool
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 26156376
Another one...
Option Explicit

Private Sub CommandButton1_Click()
    Dim col As New Collection
    col.Add "a"
    col.Add "b"
    col.Add "c"
    col.Add "d"
    ' etc.
    Dim itm As Variant
    Debug.Print "Original:"
    For Each itm In col
        Debug.Print itm
    ShuffleCollection col

    Debug.Print "Shuffled:"
    For Each itm In col
        Debug.Print itm
End Sub

Private Sub ShuffleCollection(ByVal C As Collection)
    Static seeded As Boolean
    If Not seeded Then
        seeded = True
        Randomize Timer
    End If

    Dim c2 As New Collection
    While C.Count > 0
        c2.Add C(1)
        C.Remove 1
    Dim index As Long
    While c2.Count > 0
        index = Int((c2.Count - 1 + 1) * Rnd + 1)
        C.Add c2(index)
        c2.Remove index
End Sub

Open in new window


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question