Dear Expert,
We have a problem on how to make random capture data without the duplicate for the new result with the code below. For the detail, you can find the excel as in attached file.
----
Thank for your help
Phanith
We have a problem on how to make random capture data without the duplicate for the new result with the code below. For the detail, you can find the excel as in attached file.
Code:
Sub sub_random()
Dim wbD As Workbook
Dim wsD As Worksheet
Dim wsR As Worksheet
Dim nber_rows As Long
Dim sample As Long
Dim alea As Long
Dim i As Long, j As Long, k As Long
Set wbD = ThisWorkbook
Set wsD = wbD.Worksheets("Sheet1")
nber_rows = wsD.Cells(4, 3).Value
Set wsR = wbD.Worksheets.Add
wsR.Move After:=wbD.Worksheets("Sheet1")
wsR.Name = "RenamePlease"
wsR.Cells(1, 1).Resize(1, 6).Value = wsD.Cells(9, 1).Resize(1, 6).Value
wsR.Cells(1, 1).Resize(1, 6).Interior.Color = RGB(221, 235, 247)
sample = WorksheetFunction.RoundUp(nber_rows * 0.5, 0)
Randomize
i = Int((nber_rows - 1 + 1) * Rnd + 10)
wsR.Cells(2, 1).Resize(1, 6).Value = wsD.Cells(i, 1).Resize(1, 6).Value
For j = 2 To sample
i = Int((nber_rows - 1 + 1) * Rnd + 10)
wsR.Cells(j + 1, 1).Resize(1, 6).Value = wsD.Cells(i, 1).Resize(1, 6).Value
For k = 1 To j - 1
While wsR.Cells(j + 1, 4).Value = wsR.Cells(k + 1, 4).Value
i = Int((nber_rows - 1 + 1) * Rnd + 10)
wsR.Cells(j + 1, 1).Resize(1, 6).Value = wsD.Cells(i, 1).Resize(1, 6).Value
Wend
Next k
Next j
Columns.AutoFit
End Sub
----
Thank for your help
Phanith
Attachments
Last edited: