Auto picking and pasting random samples from one sheet to another sheet.

ebineg

New Member
Joined
Feb 24, 2016
Messages
39
Hey guys,

I'm trying to pick random samples from the sheet which contains huge data ( many people data ).
I'm new to <acronym title="vBulletin" style="border-width: 0px 0px 1px; border-top-style: initial; border-right-style: initial; border-bottom-style: dotted; border-left-style: initial; border-top-color: initial; border-right-color: initial; border-bottom-color: rgb(0, 0, 0); border-left-color: initial; border-image: initial; cursor: help; color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);">VB</acronym> so any help would be greatly appreciated.

Explanation:
i have a sheet which contains huge data of many people.which looks like below:

usernameworknumber linkworked ondurationfindno find
xxxx23164576767665
yyyy24274677777666
zzzz25384778787667
yyyy34494879797668
zzzz565104980807669
xxxx786115081817670
zzzz2437125182827671
yyyy1238135283837672
zzzz1329145384847673

<colgroup><col><col span="3"><col><col span="3"></colgroup><tbody>
</tbody>

The above list goes on till 1000 rows. what i want to do is,i want to pick random samples of 8% from the whole data username wise.
if xxx user data is 250 in sheet 1 i want to take 8% sample which is 250*8/110 = 20 random samples.like wise i want to do it for all the users one by one and auto paste it in the sheet 2 like given below ( the whole row data)

xxxx23164576767665
xxxx786115081817670

<colgroup><col><col span="3"><col><col span="3"></colgroup><tbody>
</tbody>
zzzz25384778787667
zzzz565104980807669
zzzz2437125182827671
zzzz1329145384847673

<colgroup><col><col span="3"><col><col span="3"></colgroup><tbody>
</tbody>

when i do it manually using rand formula and filter options it takes much time, i'm wondering if i can automate these steps. since i do this work every day it eats lot of my time. any help on this is much appreciated !!!.

Thanks in advance :) :) :)
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Assuming your data starts in cell A1 with headers in the top row, this should work:
Code:
Public Sub PickSample()
  Const strSHEET_NAME As String = "Sheet1" '<-- Change to the name of the
  Const dblSAMPLE_RATE As Double = 0.08    '    sheet containing the data
  Const intNUM_COLUMNS As Integer = 8 '<-- This is the number of
  Dim avarSampleData() As Variant     '    columns in the data
  Dim astrColumnNames() As String
  Dim lngSampleSize As Long
  Dim lngUserCount As Long
  Dim lngRandomNo As Long
  Dim i As Integer
  Dim j As Long
  
  ReDim astrColumnNames(1 To intNUM_COLUMNS)
  astrColumnNames(1) = "username"
  astrColumnNames(2) = "work"
  astrColumnNames(3) = "number"
  astrColumnNames(4) = "link"
  astrColumnNames(5) = "worked on"
  astrColumnNames(6) = "duration"
  astrColumnNames(7) = "find"
  astrColumnNames(8) = "no find"
  
  With ThisWorkbook.Sheets(strSHEET_NAME)
    lngUserCount = .Cells(.Rows.Count, 1).End(xlUp).Row - 1
    lngSampleSize = CLng(lngUserCount * dblSAMPLE_RATE)
    ReDim avarSampleData(1 To lngSampleSize, 1 To intNUM_COLUMNS)
    
    For j = 1 To lngSampleSize
      lngRandomNo = Int(lngUserCount * Rnd() + 1)
      For i = 1 To intNUM_COLUMNS
        avarSampleData(j, i) = .Cells(lngRandomNo + 1, i).Value
      Next i
    Next j
  End With
  
  With ThisWorkbook.Sheets.Add
    .Range("A1").Resize(, intNUM_COLUMNS) = astrColumnNames()
    .Range("A2").Resize(lngSampleSize, intNUM_COLUMNS).Value = avarSampleData()
  End With
End Sub
 
Upvote 0
hi gpeacock ,

The code was great , it took 8% samples from the whole sheet regardless to the usernames .
The thing is i wanted to take samples separately for each user from the same sheet1( lets assume the sheet 1 contains 1000 rows and we have 3 users, datas of users is mixed up , assume
data of user xxx are present in 350 different rows, data of user yyy are present in 500 different rows and data of user zzz are present in 150 different rows so total = 1000 rows.
Now I din't want to take samples for that 1000 rows,instead i want to take samples for each user . first 8% sample for xxx user (350 rows),then followed by
8% sample for yyy user (500 rows), at last 8% sample for zzz user (150 rows).
so when i run the macro , i should be able to get 8% random samples for each user seperatly in the same consolidated sheet.

i hope this explains my case. it will be really helpful if i get code for the above logic.

Thanks in advance.

 
Upvote 0

Forum statistics

Threads
1,216,725
Messages
6,132,344
Members
449,719
Latest member
excel4mac

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top