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

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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