Consulting

Results 1 to 4 of 4

Thread: Solved: Help on selecting unique random words from a list.

  1. #1

    Exclamation Solved: Help on selecting unique random words from a list.

    Hello Everyone!

    Please anybody can help me on below query:
    I need to select unique random word from a given list of words.

    Example:

    Prerequsite: Assume, I have 100 words list in coloums A1:A100.

    Like:
    asf
    eft
    jhg & so on...

    Now, enter the number of unique words needed.
    Suppose - We entered 10 then, I should get 10 different/unique random words from the list of 100 words.

    Kindly help on this!

    Thanks,
    Nitin Jain

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    hi.

    try this:

    [vba]
    Sub Random_Selection_From_List()

    Dim fromRng As New Collection
    Dim cll As Range
    Dim toRand() As String
    Dim i As Long, j As Long, num As Long

    For Each cll In Range("A1:A100")
    fromRng.Add cll.Value
    Next

    num = Application.InputBox("Enter the Number of Elements", "RANDOM SELECTION", 10)

    ReDim toRand(1 To num)

    For i = LBound(toRand) To UBound(toRand)
    j = Int(Rnd * fromRng.Count) + 1
    toRand(i) = fromRng(j)
    fromRng.Remove j
    Next

    'do whatever you want with the selected values
    'my example: write array elements to range starting at J1 in the same worksheet
    Range("J1").Resize(UBound(toRand)) = Application.Transpose(toRand)

    End Sub
    [/vba]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    Thanks mancubus!

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    You are welcome.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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