Selecting Cases in Excel

Posted on 2006-05-03
Last Modified: 2010-04-23
I am currently using Microsoft Excel 2003.

I would like to have a list of data, and be able to randomly choose rows out of that list.  For example:  Say i have 5000 rows of data, I would like to be able to choose 100 random samples out of that 5000.  Then be able to extract the chosen rows either manually or have VB do it for me.   I would also like to be able to easily change that number, so one sample tell it to choose 500 rows of the 5000, then the next time be able to choose 100.

I am at a lost of how to do this, or if it is even possible in Excels VB.  I had an idea of having a macro randomly choose the amount of rows i specify, then placing an X in column A, then i could sort manually and go from there.  I would be happy with that.  

Any suggestions.  Thanks for the input.
Question by:mLuethye
    1 Comment
    LVL 6

    Accepted Solution

    Yes, this should be possible. You will need a function to which you can pass the number of rows you want and which can return an array of the selected rows or their row numbers. (If you return the rows themselves, then you also have to pass it the name of the Excel spreadsheet, of course).

    Some pseudo-ish code:

    Function GetSomeRows(numRows as Integer) As Array
    Dim selectedRows as Array

    Dim MyValue As Integer
    Randomize   ' Initialize random-number generator.

    For i as Integer = 1 to numRows
         MyValue = CInt(Int((numRows * Rnd()) + 1)) ' Generate random value between 1 and numRows.
         If Array.BinarySearch(selectedRows,MyValue)=-1 Then
              i -= 1 'to make up for not adding a duplicate value
         End If

    Return selectedRows

    End Sub

    If you want to actually pull the rows from the spreadsheet, you would have to instantiate an Excel object and build an array of the selected rows. I won't go into the details of creating the Excel object (lots of info on that here already).



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now