+ Reply to Thread
Results 1 to 24 of 24

Select Random Rows and Display on new sheet

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-18-2004
    Location
    Portland, Oregon
    MS-Off Ver
    2016
    Posts
    182

    Select Random Rows and Display on new sheet

    Hello Everyone,

    I have been searching and finally found something close to what I would like to do prior to asking you all. In post: http://www.excelforum.com/excel-prog...selection.html they did an excellent job selecting the random rows.

    I need to take it one step further, I need the selected rows to be displayed on a new sheet. The idea being that I have a quiz with hundreds of questions and I only want a random selection to appear when I execute the macro. I plan on hiding the original questions and only displaying the randomly selected ones.

    Thanks for your help in advance!

    Here is the original code from the previous post.

    Sub Random()
    
      Dim arr As Variant
      Dim nodupes As New Collection
      Dim rng As Range
      
      
      
      strr = "How many to select"
      Do
        noofcells = Application.InputBox(strr, Type:=1)
        If noofcells > Selection.Cells.Count Then strr = "You must select a number less than " & Selection.Cells.Count + 1 & ".  How many to select?"
        
      Loop Until noofcells <= Selection.Cells.Count
      ReDim arr(noofcells)
      
      
      Do
        On Error Resume Next
        arr = Evaluate("=randbetween(1," & Selection.Cells.Count & ")")
        nodupes.Add Item:=arr, Key:=CStr(arr)
        On Error GoTo 0
      Loop Until nodupes.Count = noofcells
      
     If noofcells = 1 Then
       Selection.Cells(nodupes(i)).Select
      Else
        Set rng = Selection.Cells(nodupes(1))
        For i = 2 To noofcells
          Set rng = Union(rng, Selection.Cells(nodupes(i)))
        Next i
      End If
        
     rng.Select
         
      
      With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 65535
            .TintAndShade = 0
            .PatternTintAndShade = 0
      End With
      
    End Sub
    Last edited by matt4003; 04-01-2009 at 07:10 PM.

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Select Random Rows and Display on new sheet

    Hi

    Set up and attach an example file showing where your questions will exist, and where you want the output to be placed.

    rylo

  3. #3
    Forum Contributor
    Join Date
    05-18-2004
    Location
    Portland, Oregon
    MS-Off Ver
    2016
    Posts
    182

    Re: Select Random Rows and Display on new sheet

    Hello Rylo! Thank you for your help in advance! I have attached a sample of the worksheet ("quiz") for you to see what I am talking about.

    One note, I did try to edit the code and put "rows" in place of "cells", and tried to preselect the area for the random selection to take place. But I don't think my changes worked.
    Attached Files Attached Files
    Last edited by matt4003; 03-30-2009 at 07:42 PM. Reason: more information.

  4. #4
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Select Random Rows and Display on new sheet

    Hi

    How many questions do you want returned? Is it a fixed number, or will it be asked somehow?


    rylo

  5. #5
    Forum Contributor
    Join Date
    05-18-2004
    Location
    Portland, Oregon
    MS-Off Ver
    2016
    Posts
    182

    Re: Select Random Rows and Display on new sheet

    Hello Rylo,

    It would make the most sense to me to have the number of questions picked be static in the VBA. Hopefully though I will be able to change that value just by changing the single code value.

    Let's start with 5 questions.

    Regards,
    Matt

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Select Random Rows and Display on new sheet

    Here's a start ...
    Last edited by shg; 03-31-2009 at 06:37 PM. Reason: remove attachment
    Entia non sunt multiplicanda sine necessitate

  7. #7
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Select Random Rows and Display on new sheet

    Faye,

    Unfortunately you need to post your question in a new thread, it's against the forum rules to post a question in the thread of another user. If you create your own thread, any advice will be tailored to your situation so you should include a description of what you've done and are trying to do. Also, if you feel that this thread is particularly relevant to what you are trying to do, you can surely include a link to it in your new thread.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ 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