Random Selection of Rows

rangequestion

Board Regular
Joined
Nov 21, 2016
Messages
62
Hi,
I Have the code below that randomly selects 5% of rows in the active worksheet and pastes it to another. The problem is that this macro delivers duplicate rows. Is there any way to edit the code to ensure duplicates are not in the new worksheet?

Thanks
Sub RandSelect()
Dim Qty As Long
Dim UsdRws As Long
Dim Rw As Long
Dim MasterSht As Worksheet
Dim NewSht As Worksheet
Set MasterSht = ActiveSheet
UsdRws = Cells.Find("*", after:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

Sheets.Add(after:=Sheets(Sheets.Count)).Name = "QC Sample"
Set NewSht = Sheets("QC Sample")
MasterSht.Rows(1).Copy NewSht.Range("A1")
Qty = Int(0.05 * UsdRws)

Do While Qty > 0
Rw = WorksheetFunction.RandBetween(2, UsdRws)
MasterSht.Rows(Rw).Copy NewSht.Range("A" & Rows.Count).End(xlUp).Offset(1)
Qty = Qty - 1
Loop

End Sub​
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi there.

I have added an array to save used rows, see code below.

Code:
Sub RandSelect()
Dim Qty As Long
Dim UsdRws As Long
Dim Rw As Long
Dim MasterSht As Worksheet
Dim NewSht As Worksheet
Set MasterSht = ActiveSheet
Dim UsedIndex As Long
Dim Looper As Long
Dim Dup As Boolean
UsdRws = Cells.Find("*", after:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

Sheets.Add(after:=Sheets(Sheets.Count)).Name = "QC Sample"
Set NewSht = Sheets("QC Sample")
MasterSht.Rows(1).Copy NewSht.Range("A1")
Qty = Int(0.05 * UsdRws)
ReDim Used(Qty)
UsedIndex = 0
Do While Qty > 0
Rw = WorksheetFunction.RandBetween(2, UsdRws)
Dup = False
For Looper = 0 To UsedIndex
If Used(Looper) = Rw Then
Qty = Qty + 1
Dup = True
MsgBox Rw
Exit For

End If
Next Looper

If Not Dup Then
MasterSht.Rows(Rw).Copy NewSht.Range("A" & Rows.Count).End(xlUp).Offset(1)
Used(UsedIndex) = Rw
UsedIndex = UsedIndex + 1
End If
Qty = Qty - 1
Loop

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,204
Members
453,022
Latest member
RobertV1609

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