Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Kim Kim is offline
external usenet poster
 
Posts: 284
Default Random selections

I am helping a user who has a Workbook which contains multiple worksheets of
demographic information from the state. Each worksheet is from a different
county. They are wanting to select a random number of records from each
county (worksheet) for a mailing.

I tried the rand() command but that didn't do what we needed. I then
selected all worksheets and then selected every fifth record (holding ctrl)
but wasn't able to copy to another worksheet. So I named that range, thinking
I could merge into Word or Access selecting the range, but the named range
didn't appear when I tried to import.

Any ideas?
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 5,441
Default Random selections

Try running the macro below while the workbook with the data is active. Change the line

Frac = 0.05

to whatever percent (as a decimal) that you want to select (e.g., 0.2 = 20%)

HTH,
Bernie
MS Excel MVP


Sub ConsolidateRandomSelectionFromSheetsIntoDataBase()

With Application
.DisplayAlerts = False
.EnableEvents = False
.ScreenUpdating = False
End With

Dim i As Integer
Dim myDB As Worksheet
Dim Frac As Double

Frac = 0.05

Set myDB = Worksheets.Add(Befo=Worksheets(1))
myDB.Name = "Random Selection"

Worksheets(2).Cells.Copy Worksheets(1).Cells

With Worksheets(1)
.Cells(1, 1).EntireColumn.Insert
.Cells(1, 1).Value = "County"
Intersect(.Range("A2:A" & .Rows.Count), _
.UsedRange).Value = Worksheets(2).Name
End With

For i = 3 To Worksheets.Count
With Worksheets(1)
myRow = .Cells(Rows.Count, 1).End(xlUp).Row + 1
Worksheets(i).Cells(1, 1).CurrentRegion.Offset(1) _
.Copy Worksheets(1).Cells(myRow, 2)
Intersect(.Range("A" & myRow & ":A" & .Rows.Count), _
.UsedRange).Value = Worksheets(i).Name
End With
Next i

With Worksheets(1)
.Cells(1, 1).EntireColumn.Insert
.Cells(1, 1).Value = "Select"
Intersect(.Range("A2:A" & .Rows.Count), _
.UsedRange).Formula = "=RAND()<" & Format(Frac, "0.000")
Application.CalculateFull
.Range("A:A").Value = .Range("A:A").Value
.Cells.Sort Key1:=.Range("A2"), Order1:=xlDescending, Header:=xlYes
Set myR = .Columns("A:A").Find(What:="FALSE", After:=.Range("A1"))
.Range(myR, myR.End(xlDown)).EntireRow.Delete
.Range("A:A").Delete
End With

With Application
.DisplayAlerts = True
.EnableEvents = True
.ScreenUpdating = True
End With

End Sub



"Kim" wrote in message
...
I am helping a user who has a Workbook which contains multiple worksheets of
demographic information from the state. Each worksheet is from a different
county. They are wanting to select a random number of records from each
county (worksheet) for a mailing.

I tried the rand() command but that didn't do what we needed. I then
selected all worksheets and then selected every fifth record (holding ctrl)
but wasn't able to copy to another worksheet. So I named that range, thinking
I could merge into Word or Access selecting the range, but the named range
didn't appear when I tried to import.

Any ideas?



  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 5,441
Default Random selections

I should have stated my assumptions: that the sheet name is the county name, and the data is in a
table with the headers in Row1, and data starting in row 2, cell A2.

HTH,
Bernie
MS Excel MVP


"Kim" wrote in message
...
I am helping a user who has a Workbook which contains multiple worksheets of
demographic information from the state. Each worksheet is from a different
county. They are wanting to select a random number of records from each
county (worksheet) for a mailing.

I tried the rand() command but that didn't do what we needed. I then
selected all worksheets and then selected every fifth record (holding ctrl)
but wasn't able to copy to another worksheet. So I named that range, thinking
I could merge into Word or Access selecting the range, but the named range
didn't appear when I tried to import.

Any ideas?



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using selections in VBA GeorgeJ Excel Discussion (Misc queries) 6 August 28th 07 06:52 PM
help with selections josswallace Excel Discussion (Misc queries) 1 January 7th 06 05:45 PM
Non-random numbers generated by excel's data analysis random gener Allie Excel Worksheet Functions 10 September 17th 05 06:19 AM
Non-random numbers generated by excel's data analysis random gener Harlan Grove Excel Discussion (Misc queries) 2 September 13th 05 04:06 PM
How do I find random number in list of random alpha? (Position is. jlahealth-partners Excel Discussion (Misc queries) 0 February 8th 05 05:31 PM


All times are GMT +1. The time now is 07:56 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"