Solved

How to select a random percentage of a spreadsheet to remove for sample

Posted on 2016-10-12
9
40 Views
Last Modified: 2016-10-14
I've got a mailing list for a client that want's to randomly remove a percentage of the data for what their calling a  "dormant" list. I've tried inserting a new column "A" and using the rand formula to assign a random number but I'm having no luck. Any help would be greatly appreciated! I've attached a demo mailing list. I would like to be able to randomly remove 20% of the list and copy them to sheet2. I will be doing this on a ongoing basic so I was thinking a macro?
Excel-Demo-List.xls
0
Comment
Question by:Dan Purcell
  • 5
  • 4
9 Comments
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 41841107
While you could add a RANDBETWEEN function to help flag 20% of the items, the issue will be that it won't always guarantee that you'll get exactly 20% identified.  That's where an iterative process in a macro comes in.  And since you want to move the items to another sheet, all the more reason for a macro.

I've added code to your sample workbook that will add a temporary column with a random value generator,  =RANDBETWEEN(1,5) , then iteratively recalculate the workbook until 20% of those values are equal to 5.  Then it filters and copies over the items to a new sheet - [Sample] - then removes them from the first sheet.

The macro name is "Move_Entries" and can be access by pressing [Alt]+[F8] and selecting that macro from the list.

Note:  this is a destructive process!  The items are MOVED from the [AddressData] sheet.  If you already have a [Sample] sheet of data and run the process again, the sample items are lost. Forever.

-Glenn
EE-Excel-Demo-List.xls
1
 

Author Comment

by:Dan Purcell
ID: 41841933
Perfect!  

Two questions.

 If I need to change the percentage from 20% to 10% I change this line of the macro?:

int20Pct = Round((intLastRow - 1) * 0.2, 0)    from 0.2 to 0.1?

Second question:

My list may not always be the same number of columns. Some lists may have more. What will I need to change?
0
 

Author Comment

by:Dan Purcell
ID: 41841935
I tried to give you best solution but it wanted to close the question. I'll give you it after
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 27

Expert Comment

by:Glenn Ray
ID: 41841946
1) Chang the percentage:  yes, that change is correct, but you'll also have to change the RANDBETWEEN range from (1,5) to (1,10).  There's another algorithm that can be used if you think you'll chang regularly where you can input the percentage at run time. Let me know if you might need that.

2) The code assumed data in columns A:G; any reference to such would have to be changed.  In addition, the temporary "flag" column is in H (one column to the right); that would also need to be changed.  If you need a more open-ended solution, I could revise the code to detect the columns used and run from there.
0
 

Author Comment

by:Dan Purcell
ID: 41842102
You folks that help out here are amazing! This is a new customer and will have on going work with them. They will have different clients so I would imagine the parameters may vary. If you could add the open-ended I will forever be in your debt
0
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 41842886
I've modified the code to
1) allow for any percentage to be specified ("intSamplePct" at the top of the code); currently set to 20
2) allow for any size (columns) of the original data.  The temporary column is now inserted in column A and then removed.

See the revised workbook.
EE-Excel-Demo-List.xls
0
 

Author Comment

by:Dan Purcell
ID: 41843608
You Rock! That worked perfect.

Thank you, thank you
0
 

Author Closing Comment

by:Dan Purcell
ID: 41843609
Thank you very much Glenn.
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 41844407
You're Welcome, Dan.  Glad I could help.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

765 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