Consulting

Results 1 to 8 of 8

Thread: Randomly select from a range

  1. #1
    VBAX Regular babsc01's Avatar
    Joined
    Jun 2004
    Location
    Southaven, MS
    Posts
    53
    Location

    Randomly select from a range

    Howdy...this will probably be an easy one. I have a range of cells containing numbers. I want to randomly select a certain quantity of those numbers, non-repeating, and display the result in another range.

    Any VBA or formula ideas? Thanks!

  2. #2
    VBAX Contributor Richie(UK)'s Avatar
    Joined
    May 2004
    Location
    UK
    Posts
    188
    Location
    Hi b,

    Tushar Mehta has some useful routines here:

    http://www.tushar-mehta.com/excel/ne...ction/vba.html

  3. #3
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Hi babsc01,

    I didn't actually look at Richie's link, so a similar thing may be there, but you could also use a sub like:

    Sub LetsGiveItATry()
     Dim InputRange As Range, OutputRange As Range, CLL As Range
     Dim CellsColl As New Collection, i As Long, j As Long
    Set InputRange = Range("A1:A35")
     Set OutputRange = Range("B1:B9")
    If OutputRange.Cells.Count > InputRange.Cells.Count Then
      Msgbox "Output range bigger than input range. Please re-define."
      Exit Sub
     End If
     For Each CLL In InputRange.Cells
      CellsColl.Add CLL
     Next CLL
     Randomize
     For i = 1 To OutputRange.Cells.Count
      j = Int(Rnd() * CellsColl.Count + 1)
      OutputRange.Cells(i) = CellsColl(j)
      CellsColl.Remove j
     Next
    End Sub
    Just define the InputRange and the OutputRange, should take care of it all for you.
    Matt

  4. #4
    VBAX Regular babsc01's Avatar
    Joined
    Jun 2004
    Location
    Southaven, MS
    Posts
    53
    Location
    This will work...thanks so much, Matt.

  5. #5
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Glad to hear it! You can mark it as Solved using "Thread Tools" right above your first message. That way everyone will know you're all set.

    This thread must have been edited to remove the Randomize discussion. That's too bad, it was nice seeing people's views trying to figure out how excel works.

  6. #6
    VBAX Regular babsc01's Avatar
    Joined
    Jun 2004
    Location
    Southaven, MS
    Posts
    53
    Location
    I think it was deleted, too. Almost seemed like it was turning into a pretty heated discussion.

  7. #7
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    My favorite kind

  8. #8
    VBAX Contributor Aaron Blood's Avatar
    Joined
    Sep 2004
    Location
    Palm Beach, Florida, USA
    Posts
    130
    Location
    Quote Originally Posted by babsc01
    I think it was deleted, too. Almost seemed like it was turning into a pretty heated discussion.
    ...not heated. Too much caffeine in the afternoon I think.

    The ending was cool though... Involved light sabers and acrobatics.

    Some errant force lightning must've zapped a few of the innocently bystanding postings. It happens sometimes when the Jedi clash.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •