私は2つの列を持ちます.1つはユーザー名で、もう1つはそれぞれ固有のユーザーの決定です。たとえば、ユーザー名がRohitで、すべてが10%ランダムです。ユーザー決定がNOだった行10%同じユーザーのすべての行がNOの場合、このコードでは列ユーザーのみが固有の名前の10%データを提供しています。条件を満たす場合にランダムな行をコピー
Sub Random10_EveryName()
Randomize 'Initialize Random number seed
Application.ScreenUpdating = False
'Copy Sheet1 to new sheet
Sheets("Sheet1").Copy After:=Sheets(Sheets.Count)
'Clear old data in Sheet 2
Sheets(2).Cells.ClearContents
'Determine Number of Rows in Sheet1 Column A
numRows = Sheets(Sheets.Count).Cells(Rows.Count, _
"A").End(xlUp).Row
'Sort new sheet by Column E
Sheets(Sheets.Count).Cells.Sort _
key1:=Sheets(Sheets.Count).Range("O1:D" & numRows), _
order1:=xlAscending, Header:=xlYes
'Initialize numNames & startRow variable
numNames = 1
startRow = 2
'Loop through sorted names, count number of current Name
For nameRows = startRow To numRows
If Sheets(Sheets.Count).Cells(nameRows, "D") = _
Sheets(Sheets.Count).Cells(nameRows + 1, "D") Then
numNames = numNames + 1
Else:
endRow = startRow + numNames - 1
'Generate Random row number within current Name Group
nxtRnd = Int((endRow - startRow + 1) * _
Rnd + startRow)
'Copy row to Sheet2, Delete copied Name
dstRow = Sheets(2).Cells(Rows.Count, "A").End(xlUp).Row + 1
Sheets(Sheets.Count).Rows(nxtRnd).EntireRow.Copy _
Destination:=Sheets(2).Cells(dstRow, 1)
Sheets(Sheets.Count).Cells(nxtRnd, "D").ClearContents
'Set Start Row for next Name Group, reset numNames variable
startRow = endRow + 1
numNames = 1
End If
Next
'Sort new sheet by Column O
Sheets(Sheets.Count).Cells.Sort _
key1:=Sheets(Sheets.Count).Range("O1:E" & numRows), _
order1:=xlAscending, Header:=xlYes
'Determine Number of Remaining Names in new sheet Column O
numNamesleft = Sheets(Sheets.Count).Cells(Rows.Count, _
"E").End(xlUp).Row - 1
'Determine 10% of total entries from Sheet1
percRows = _
WorksheetFunction.RoundUp((numRows - 1) * 0.2, 0)
'Determine how many extra rows are needed to reach 10% of total
unqNames = Sheets(2).Cells(Rows.Count, _
"E").End(xlUp).Row - 1
extRows = percRows - unqNames
'Warn user if number of Unique Names exceeds 10% of Total Entires
If extRows < 0 Then
MsgBox "Number of Unique Names Exceeds 10% of Total Entries"
'Delete new sheet
Application.DisplayAlerts = False
Sheets(Sheets.Count).Delete
Application.DisplayAlerts = True
Exit Sub
End If
'Extract Random entries from remaining names to reach 10%
'
'Allocate elements in Array
ReDim MyRows(extRows)
'Create Random numbers and fill array
For nxtRow = 1 To extRows
getNewRnd:
'Generate Random row numbers within current Name Group
nxtRnd = Int((numNamesleft - 2 + 1) * _
Rnd + 2)
'Loop through array, checking for Duplicates
For chkRnd = 1 To nxtRow
'Get new number if Duplicate is found
If MyRows(chkRnd) = nxtRnd Then GoTo getNewRnd
Next
'Add element if Random number is unique
MyRows(nxtRow) = nxtRnd
Next
'Loop through Array, copying rows to Sheet2
For copyrow = 1 To extRows
dstRow = Sheets(2).Cells(Rows.Count, "A").End(xlUp).Row + 1
Sheets(Sheets.Count).Rows(MyRows(copyrow)).EntireRow.Copy _
Destination:=Sheets(2).Cells(dstRow, 1)
Next
'Delete new sheet
Application.DisplayAlerts = False
Sheets(Sheets.Count).Delete
Application.DisplayAlerts = True
End Sub
(nSamplesに1)のReDim rndNumbers用範囲外の添字を取得している限り –
GetRandomSampleは() 'の結果である必要があり、そのターンのnSamples'引数値、'としてゼロを受け 'ので、おそらくそれはです'nCells * perc'が1より小さい場合は' nPerc = Int(nCells * perc) 'です。したがって、' nPerc = WorksheetFunction.RoundUp(nCells * perc、0) 'で変更しました。編集コード – user3598756
を参照してください。 .. –