?
Solved

Selecting Cases in Excel

Posted on 2006-05-03
3
Medium Priority
?
172 Views
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.
0
Comment
Question by:mLuethye
1 Comment
 
LVL 6

Accepted Solution

by:
ctm5 earned 1000 total points
ID: 16600656
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
         selectedRows.Add(MyValue)
     Else
          i -= 1 'to make up for not adding a duplicate value
     End If
Next

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).

ctm5

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

831 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