Post by carlo_excel_52rettifico: Il civico è separato dall'indirizzo
Post by carlo_excel_52-il DB è organizzato per comune/indirizzo/nome_residente
-diciamo che dovrebbe scegliere le vie con almeno 10 abitanti
-se non c'è un numero sufficiente di abitanti tralascia e passa ad
un'altra via.
Ciao Carlo.
Allora.
Ho supposto che i dati, comune/indirizzo/nome_residente,
siano rispettivamente nelle colonne A B C.
La riga 1 contiene le intestazioni.
Ho supposto che la colonna E sia disponibile a raccogliere
l'output della macro.
Questa la macro:
----------------------------------------
Sub stessastrada()
Dim v(1 To 2000, 1 To 200) As Long
Dim nrn(1 To 2000) As Long
Dim cp(1 To 2000) As String
Dim suff() As Long
Dim scelti() As Boolean, si As Boolean
Dim r As Long, x As Long, k As Long
Dim w As Long, t As Long, cop As Long
Dim xn As Long, u As Long, nv As Long
Dim minimo As Integer, q As Integer
Dim z As Integer
Dim g As Variant
Dim c As String, via As String
Dim n As String, coppia As String
Range("E1:E100").ClearContents
minimo = 10
GoSub prepara
Randomize Time
x = Int(Rnd() * t) + 1
cop = suff(x)
g = Split(cp(cop), ":")
Cells(1, 5) = g(0)
Cells(2, 5) = g(1)
k = 3
w = nrn(cop)
ReDim scelti(1 To w)
For q = 1 To minimo
xn = Int(Rnd() * (w + 1 - q)) + 1
z = 0
For u = 1 To w
If Not (scelti(u)) Then
z = z + 1
If z = xn Then
scelti(u) = True
k = k + 1
Cells(k, 5) = Cells(v(cop, u), 3)
Exit For
End If
End If
Next
Next
Exit Sub
prepara:
nv = 0
r = 2
While Cells(r, 2).Value <> ""
c = Cells(r, 1)
via = Cells(r, 2)
n = Cells(r, 3)
coppia = c & ":" & via
si = False
For x = 1 To nv
If cp(x) = coppia Then
si = True
Exit For
End If
Next
If si = False Then
nv = nv + 1
cp(nv) = coppia
x = nv
End If
nrn(x) = nrn(x) + 1
v(x, nrn(x)) = r
r = r + 1
Wend
For x = 1 To nv
If nrn(x) >= minimo Then
t = t + 1
End If
Next
ReDim suff(1 To t)
t = 0
For x = 1 To nv
If nrn(x) >= minimo Then
t = t + 1
suff(t) = x
End If
Next
Return
End Sub
----------------------------------
Sostituisci la riga con
minimo = 10
con un altro numero oppure inserisci una
richiesta di input.
I risultati:
E1: il paese
E2: la via
Da E4 in giù i nomi (casuali) con lo stesso indirizzo.
So benissimo che è migliorabile, ma è quello che
mi è venuto in mente e che sono riuscito a
realizzare nel poco tempo che ho a disposizione.
Aspettando il feed back ...
()-- cucchiaino