请各位高手看看,如何实现避免随机选择重复的数据?
如果我不用Scripting Dictionary,就只用for next 或者do loop,可以实现避免随机选择里重复的结果吗?Range("_Data")里面是我的数据,我现在需要随便输一个数字,指定随机选择多少个项目,然后项目不能重复。
我用这个
If WorksheetFunction.CountIf(selectedrng, Cells(11 + i, "B").Value) > 1 Then
n = n + 1
num = num + 1
Else
来检查重复项目并增加循环,这样对吗?
Public datarng As Range
Public selectedrng As Range
Sub HighlithtSelected()
Set datarng = Range("_Data")
Dim num As Integer
num = Application.InputBox("Please Input the Number of Selected Items")
Dim i As Integer
Dim n As Integer
For i = 1 To num
n = Application.WorksheetFunction.RandBetween(1, datarng.Rows.Count)
Set selectedrng = datarng.Cells(n, 1)
Cells(11 + i, "B").Value = selectedrng.Value
If WorksheetFunction.CountIf(selectedrng, Cells(11 + i, "B").Value) > 1 Then
n = n + 1
num = num + 1
Else
Cells(11 + i, "B").Value = selectedrng.Value
End If
Next i
End Sub --------------------编程问答-------------------- 可以,就是速度会慢。
为什么不用Scripting Dictionary?
补充:VB , VBA