Sub SelectRandomSample()
Dim Population As Range
Dim lastRow As Long, firstrow As Long
Dim sampleSize As Long
Dim unique As Boolean
Dim i As Long, d As Long, n As Long
Sheets("Sheet2").Range("G:G").Clear
Sheets("Sheet2").Range("H:H").Clear
Set Population = Application.InputBox("추출할 샘플의 범위를 지정하세요.", Type:=8)
sampleSize = Application.InputBox("샘플의 갯수를 입력하세요.", Type:=1)
If Population.Count < sampleSize Then
MsgBox "샘플의 개수는 범위를 초과할 수 없습니다.", vbOKOnly + vbInformation
Exit Sub
End If
Set P = Population
lastRow = P.Rows.Count + P.Row - 1
firstrow = P.Row
n = Application.WorksheetFunction.RandBetween(firstrow, lastRow)
For i = 1 To sampleSize
Do
unique = True
n = Application.WorksheetFunction.RandBetween(firstrow, lastRow)
For d = 1 To i - 1
If Cells(d, 7) = n Then
unique = False
Exit For
End If
Next d
If unique = True Then
Exit Do
End If
Loop
Sheets("Sheet2").Select
Cells(i + 1, 4) = n
Cells(i + 1, 5) = Application.WorksheetFunction.Index(Population, n)
Next i
Sheets("Sheet2").Select
End Sub