
Sub CasualiUnivoci()
Dim Arr As New Collection
Dim n As Long, Casuale As Long
Estrazioni = CInt(InputBox( "Quanti nominativi su 900 max vuoi estrarre ?" ))
On Error GoTo fine
For n = 1 To Estrazioni
Randomize
Casuale = Int((900 * Rnd) + 1)
Arr.Add Casuale, CStr(Casuale)
fine:
If Err.Number = 457 Then
n = n - 1
End If
Resume Next
Next n
For n = 1 To Arr.Count
Cells(n, 2).Value = Cells(Arr(n), 1).Value
Next
Set Arr = Nothing
End Sub |
Option Explicit
Function casuale_senza_dups(vettore As Variant, num_estrazioni As Long) As String
'verificato OK sia con range che con vettori - 18/3/2013
Dim i As Integer, vect() As Variant
Dim r1 As Long, r2 As Long, tmp As Variant
Dim s As String, lim_inf As Integer, lim_sup As Integer
Randomize Timer
'nb fallisce se range.column > 1
If TypeName(vettore) = "Range" Then
vect = WorksheetFunction.Transpose(vettore)
lim_sup = UBound(vect)
Else
vect = vettore
lim_sup = UBound(vect) + 1
End If
lim_inf = LBound(vect)
'shuffle
For i = 1 To 1000
r1 = Int(Rnd * lim_sup) + lim_inf
r2 = Int(Rnd * lim_sup) + lim_inf
'swap
tmp = vect(r1)
vect(r1) = vect(r2)
vect(r2) = tmp
Next
For i = lim_inf To (num_estrazioni - 1 + lim_inf)
If i >= lim_sup - (-lim_inf) Then Exit For
s = s & vect(i) & "-"
Next
If s <> "" Then s = Left(s, Len(s) - 1)
casuale_senza_dups = s
End Function
|
Option Explicit
Sub CasualiUnivoci()
Dim Arr As Collection
Dim n As Long, Casuale As Long
Dim Estrazioni As Integer
Estrazioni = Application.InputBox("Quanti nominativi su 900 max vuoi estrarre ?", Type:=1)
Set Arr = New Collection
On Error Resume Next
Do
Casuale = (900 * Rnd) + 1
Arr.Add Casuale
Loop Until Arr.Count = Estrazioni
For n = 1 To Arr.Count
Cells(n, 3).Value = Cells(Arr(n), 1).Value
Next
Set Arr = Nothing
End Sub |
If Err.Number = 457 Then n = n - 1 End If |
Dim v As Range
For Each v In [b1:b900]
[a1:a900].Replace v, "", xlWhole
Next |
Function estrai(lista As Variant, num_estrazioni As Integer) As Variant
Dim i As Integer, k() As Long, r1 As Integer, r2 As Integer, tmp As Integer
'da una lista di numeri ordinata in modo casuale estrae il numero di estrazioni specificato
'restituisce Falso se si chiedono più estrazioni degli elementi contenuti
'for each k in estrai(array(1,2,3,4,5),3):?k;:next
If num_estrazioni > UBound(lista) + 1 Or num_estrazioni <= 0 Then
estrai = Array(False)
Exit Function
End If
Randomize Timer
'shuffle
For i = 1 To 1000
r1 = Int(Rnd * UBound(lista)) + LBound(lista)
r2 = Int(Rnd * UBound(lista)) + LBound(lista)
'swap
tmp = lista(r1)
lista(r1) = lista(r2)
lista(r2) = tmp
Next
ReDim k(UBound(lista))
For i = 0 To num_estrazioni - 1
k(i) = lista(i)
Next
ReDim Preserve k(i - 1)
estrai = k()
End Function
|
