我在A列中有一个数据列表,同样也有重复的数据。我必须创建一个宏来获得10%的数据。我尝试使用Rand()和其他一些功能,但没有成功。请任何帮助。
A栏
Brick John
Brick Matt
Brick Ted
目标约翰
目标马特
目标泰德
目标泰德
Tiger Ted
老虎约翰Tiger Matt
Tiger Matt
Code Ted
Code Matt
Code Matt
Code Ted
Code John
现在从上面的列表中我得到10%的随机样本,如果我得到重复,那就没关系 但我必须得到所有砖块,老虎,代码和目标。
请帮忙
答案 0 :(得分:2)
这将采样并将样本放在 B
列中Public Sub scramble(InOut() As String)
Dim I As Long, J As Long, Low As Long, Hi As Long
Dim Temp
ReDim Helper(LBound(InOut) To UBound(InOut)) As Double
Randomize
Low = LBound(Helper)
Hi = UBound(Helper)
For I = Low To Hi
Helper(I) = Rnd()
Next
J = (Hi - Low + 1) \ 2
Do While J > 0
For I = Low To Hi - J
If Helper(I) > Helper(I + J) Then
Temp = Helper(I)
Helper(I) = Helper(I + J)
Helper(I + J) = Temp
Temp = InOut(I)
InOut(I) = InOut(I + J)
InOut(I + J) = Temp
End If
Next I
For I = Hi - J To Low Step -1
If Helper(I) > Helper(I + J) Then
Temp = Helper(I)
Helper(I) = Helper(I + J)
Helper(I + J) = Temp
Temp = InOut(I)
InOut(I) = InOut(I + J)
InOut(I + J) = Temp
End If
Next I
J = J \ 2
Loop
End Sub
Sub samples()
Dim N As Long, N2 As Long, s() As String
N = Cells(Rows.Count, "A").End(xlUp).Row
N2 = N / 10
ReDim s(1 To N)
For I = 1 To N
s(I) = Cells(I, 1).Value
Next I
Call scramble(s)
For I = 1 To N2
Cells(I, 2).Value = s(I)
Next I
End Sub