+ Reply to Thread
Results 1 to 4 of 4

random selection from list - PLEASE HELP

  1. #1
    Registered User
    Join Date
    03-26-2012
    Location
    Hull, England
    MS-Off Ver
    Excel 2010
    Posts
    8

    random selection from list - PLEASE HELP

    Hi, I need to be able to randomly select 2 cells in a row from a list. For example:

    Name | Age
    Jim | 30
    Peter | 25
    Angela | 22

    I have the macro below which can randomly select the name into varRandomItems(i) but I need to be able to also select the age from the neighbouring cell as well. Can anyone see how it could be modified? Many thanks.

    Sub PickRandomItemsFromList()

    Const nItemsToPick As Long = 5
    Const nItemsTotal As Long = 3

    Dim rngList As Range
    Dim varRandomItems() As Variant
    Dim i As Long

    Set rngList = Range("A1").Resize(nItemsTotal, 1)

    ReDim varRandomItems(1 To nItemsToPick)
    For i = 1 To nItemsToPick
    varRandomItems(i) = rngList.Cells(Int(nItemsTotal * Rnd + 1), 1)
    Next i

    End Sub

  2. #2
    Registered User
    Join Date
    01-14-2004
    Posts
    6

    Re: random selection from list - PLEASE HELP

    Hi,

    Just store the random number prior to the selection...

    Please Login or Register  to view this content.
    Gazza
    Last edited by gazzabj; 03-26-2012 at 10:47 AM.

  3. #3
    Registered User
    Join Date
    03-26-2012
    Location
    Hull, England
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: random selection from list - PLEASE HELP

    Works perfectly! Massive thanks Gazza

  4. #4
    Valued Forum Contributor marreco's Avatar
    Join Date
    07-02-2011
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    1,862

    Re: random selection from list - PLEASE HELP

    Staff read the rules, place the code between the tags.

    Where was solucionadoentão mark your post as SOLVED
    "No xadrez nem sempre a menor dist?ncia entre dois pontos ? uma linha reta" G. Kasparov.

    If your problem is solved, please say so clearly, and mark your thread as Solved: Click the Edit button on your first post in the thread, Click Go Advanced, select b from the Prefix dropdown, then click Save Changes. If more than two days have elapsed, the Edit button will not appear -- ask a moderator to mark it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1