Sample Data

maheshmaxi

Active Member
Joined
Dec 16, 2008
Messages
252
Hi,

I want to take sample data from a huge data for audit purpose. The data has columns "User" and "Category", Unique Users - 45 and Unique Category - 27 (number of unique users and category may slightly vary day by day). I need to take 50 sample records out of 5000+ data. I should cover all Users and Category, but shouldn't exceed 50 records for sample (no problem if user and/or category is repeated withing 50 records).

Can someone help me on this?
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
.
Code:
Option Explicit


Sub Random20()


Randomize 'Initialize Random number seed
Dim MyRows() As Long ' Declare dynamic array.
Dim numRows, percRows, nxtRow, nxtRnd, chkRnd, copyRow As Long
'Determine Number of Rows in Sheet1 Column A
numRows = Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
'Get 20% of that number
percRows = 50                                           '<<---------------   Number of random rows to be copied
'Allocate elements in Array
ReDim MyRows(percRows)


'Create Random numbers and fill array
    For nxtRow = 1 To percRows
getNew:
        'Generate Random number
        nxtRnd = Int((numRows) * Rnd + 1)
        'Loop through array, checking for Duplicates
            For chkRnd = 1 To nxtRow
                'Get new number if Duplicate is found
                If MyRows(chkRnd) = nxtRnd Then GoTo getNew
            Next
        'Add element if Random number is unique
        MyRows(nxtRow) = nxtRnd
    Next
    
'Loop through Array, copying rows to Sheet2
    For copyRow = 1 To percRows
        Sheets(1).Rows(MyRows(copyRow)).EntireRow.Copy _
        Destination:=Sheets(2).Cells(copyRow, 1)            '<<---------------   Change sheet to copy to here
    Next
    
End Sub


Download link : https://www.amazon.com/clouddrive/share/dNUgW3bQzq5eoFqYehkJzrL9Izx2qZV4C84rhZ2Borw
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

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