
Sub datecasuali() Dim riga, k, caselle, n, valmax, k1, mese, mesi, c As Integer Dim m As Double caselle = Cells(2, 2) 'numero ci celle da utilizzare mesi = Cells(3, 2) 'mesi tot mese = Cells(8, 3) 'mese da cui iniziare riga = 13 c = 0 'contatore k = 1 'contatore k1 = 1 'contatore valmax = 29 m = caselle / mesi While k <= mesi While k1 <= m c = c + 1 n = Int(Rnd * valmax + 1) Cells(riga, 1).HorizontalAlignment = xlLeft Cells(riga, 1).VerticalAlignment = xlCenter Cells(riga, 1) = c Cells(riga, 2) = n & "/" & mese & "/2012" Cells(riga, 2).HorizontalAlignment = xlLeft Cells(riga, 2).VerticalAlignment = xlCenter riga = riga + 1 k1 = k1 + 1 Wend k1 = 0 mese = mese + 1 k = k + 1 Wend End Sub |
Dim riga, k, caselle, n, valmax, k1, mese, mesi, c, anno As Integer Dim m As Double caselle = Cells(2, 2) mesi = Cells(3, 2) mese = Cells(8, 3) anno = 2012 riga = 13 c = 0 k = 1 k1 = 1 valmax = 29 m = caselle / mesi Randomize (3) While k <= mesi While k1 <= m If mese = 13 Then mese = 1 c = c + 1 n = Int(Rnd * valmax + 1) Cells(riga, 1).HorizontalAlignment = xlLeft Cells(riga, 1).VerticalAlignment = xlCenter Cells(riga, 1) = c Cells(riga, 2).NumberFormat = "m/d/yyyy" Cells(riga, 2) = CDate(n & "/" & mese & "/" & anno) Cells(riga, 2).HorizontalAlignment = xlLeft Cells(riga, 2).VerticalAlignment = xlCenter riga = riga + 1 k1 = k1 + 1 anno = anno + 1 Else c = c + 1 n = Int(Rnd * valmax + 1) Cells(riga, 1).HorizontalAlignment = xlLeft Cells(riga, 1).VerticalAlignment = xlCenter Cells(riga, 1) = c Cells(riga, 2).NumberFormat = "m/d/yyyy" Cells(riga, 2) = CDate(n & "/" & mese & "/" & anno) Cells(riga, 2).HorizontalAlignment = xlLeft Cells(riga, 2).VerticalAlignment = xlCenter riga = riga + 1 k1 = k1 + 1 End If Wend k1 = 0 mese = mese + 1 k = k + 1 Wend End Sub |
Option Explicit
Sub datecasuali()
Dim riga As Integer, caselle As Integer, n As Integer, mese As Integer, mesi As Integer, anno As Integer
Dim m As Integer, num As Integer
Randomize Timer
caselle = [B2]
mesi = [B3]
mese = [C8]
anno = 2012
riga = 13
m = caselle / mesi
[A13].CurrentRegion.ClearContents
For riga = 1 To caselle
For num = 1 To m
Cells(riga + 13, 1) = riga
n = Estratto
If Not IsDate(n & "/" & mese & "/" & anno) Then
n = 1
End If
Cells(riga + 13, 2) = CDate(n & "/" & mese & "/" & anno)
Next
mese = mese + 1
If mese > 12 Then
mese = 1
anno = anno + 1
End If
Next
With [A13].CurrentRegion.Cells
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlCenter
End With
End Sub
Private Function Estratto() As Integer
' variabile statica per conservare i valori
Static numeri(31) As Integer
Dim i As Integer, e As Integer, n As Integer
If numeri(0) = 0 Then ' se la matrice non è ancora inizializzata
For i = 1 To 31
numeri(i) = i
Next
numeri(0) = 31 ' il numero massimo per la randomizzazione
End If
Randomize
'estrai un numero entro il massimo
e = Int(Rnd() * numeri(0) + 1)
'scambi le posizioni, per non estrarlo più
i = numeri(numeri(0))
numeri(numeri(0)) = numeri(e)
numeri(e) = i
numeri(0) = numeri(0) - 1
Estratto = e
End Function
|
If Not IsDate(n & "/" & mese & "/" & anno) Then
n = 1
End If |
Option Explicit
Sub datecasuali()
Dim riga As Integer, caselle As Integer, n As Integer, mese As Integer, mesi As Integer, anno As Integer
Dim m As Integer, num As Integer
Dim s As String, gio As Integer, mes As Integer
Randomize Timer
caselle = [B2]
mesi = [B3]
mese = [C8]
anno = 2012
riga = 13
m = caselle / mesi
[A13].CurrentRegion.ClearContents
For riga = 1 To caselle
For num = 1 To m
Cells(riga + 12, 1) = riga
mes = Int(Rnd * mesi) + 1
Do
gio = Int(Rnd * Day(DateSerial(anno, mes + 1, 0))) + 1
Loop Until InStr(s, Format(gio, "00") & "/" & Format(mes, "00")) = 0
s = s & Format(gio, "00") & "/" & Format(mes, "00") & " "
Cells(riga + 12, 2) = CDate(gio & "/" & mes & "/" & anno)
Next
mese = mese + 1
If mese > 12 Then
mese = 1
anno = anno + 1
End If
Next
With [A13].CurrentRegion.Cells
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlCenter
End With
[B13:B50].Sort key1:=[B13], header:=xlNo
End Sub |
Option Explicit
Sub datecasuali()
Dim caselle As Integer, mesi As Integer, mese As Integer, anno As Integer, rip As Integer
Dim u As Integer, n As Integer
Dim riga As Integer, g_min As Integer, g_max As Integer
Dim c As Integer, gio As Integer, n_anno As Integer, a As Integer, m As Integer
Dim cas(), num As Variant, t As Boolean
caselle = [B2]
mesi = [B3]
mese = [C8]
anno = 2012
riga = 13
m = caselle / mesi
[A13].CurrentRegion.ClearContents
For rip = 0 To mesi - 1
n_anno = mese + rip
If n_anno > 12 Then
n_anno = 1 + a
anno = anno + 1
a = a + 1
End If
u = Day(DateSerial(anno, n_anno + 1, 0))
g_min = 1
g_max = u
Randomize (1)
For n = 1 To m
ReDim cas(n)
c = c + 1
Do
gio = Int(Rnd() * (g_max - g_min + 1)) + g_min
For Each num In cas
If gio = num Then t = True: Exit Sub
Next
Loop Until t = False
cas(n) = gio
Cells(c + 12, 1) = riga
Cells(riga, 2) = CDate(gio & "/" & n_anno & "/" & anno)
riga = riga + 1
Next n
Next
With [A13].CurrentRegion.Cells
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlCenter
End With
[B13:B50].Sort key1:=[B13], Header:=xlNo
End Sub
|
Option Explicit
Sub datecasuali()
Dim caselle As Integer, mesi As Integer, mese As Integer, anno As Integer, rip As Integer
Dim u As Integer, n As Integer
Dim riga As Integer, g_min As Integer, g_max As Integer
Dim c As Integer, gio As Integer, n_anno As Integer, a As Integer, m As Integer
Dim cas(), num As Variant, t As Boolean
caselle = [B2]
mesi = [B3]
mese = [C8]
anno = 2012
riga = 13
m = caselle / mesi
[A13].CurrentRegion.ClearContents
For rip = 0 To mesi - 1
n_anno = mese + rip
If n_anno > 12 Then
n_anno = 1 + a
anno = anno + 1
a = a + 1
End If
u = Day(DateSerial(anno, n_anno + 1, 0))
g_min = 1
g_max = u
Randomize (1)
ReDim cas(m)
For n = 1 To m
c = c + 1
Do
gio = Int(Rnd() * (g_max - g_min + 1)) + g_min
t = False
For Each num In cas
If gio = num Then t = True: Exit For
Next
Loop Until t = False
cas(n) = gio
Cells(c + 12, 1) = riga
Cells(riga, 2) = CDate(gio & "/" & n_anno & "/" & anno)
riga = riga + 1
Next n
Next
With [A13].CurrentRegion.Cells
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlCenter
End With
[B13:B50].Sort key1:=[B13], Header:=xlNo
End Sub
|
Option Explicit
Sub datecasuali()
Dim caselle As Integer, mesi As Integer, mese As Integer, anno As Integer
Dim riga As Integer, gio As Integer, m As Integer, gruppo() As Integer, s As String
Dim num_per_gruppo As Integer, i As Integer, j As Integer, esiste As Boolean
Randomize Timer
caselle = [B2] 'elementi da dividere nei gruppi (mesi)
mesi = [B3] 'gruppi max
mese = [C8] 'mese di partenza
anno = 2012 'anno di partenza
riga = 13 'cella da cui partire per visualizzare
num_per_gruppo = Int(caselle / mesi) 'quanti di uno stesso mese per ogni gruppo
ReDim gruppo(mesi) As Integer 'quanti gruppi vengono formati
For i = 1 To mesi
gruppo(i) = num_per_gruppo
Next
For i = 1 To (caselle Mod mesi)
gruppo(i) = gruppo(i) + 1
Next
[A13].CurrentRegion.ClearContents
For i = 1 To mesi 'crea i gruppi
s = ""
m = mese + i - 1
If m > 12 Then
m = 1
anno = anno + 1
End If
For j = 1 To gruppo(i) 'estrae tot elementi per mese
Do
gio = Int(Rnd * Day(DateSerial(anno, m + 1, 0)) + 1) 'calcola un giorno da 1 alla data finale del mese specificato
esiste = InStr(s, gio)
Loop Until Not esiste
s = s & gio & " "
Cells(riga, 1) = riga - 12
Cells(riga, 2) = CDate(gio & "/" & m & "/" & anno)
riga = riga + 1
Next
Next
With [A13].CurrentRegion.Cells
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlCenter
End With
[B13:B50].Sort key1:=[B13], Header:=xlNo
End Sub |
Option Explicit
Sub datecasuali()
Dim caselle As Integer, mesi As Integer, mese As Integer, anno As Integer, rip As Integer
Dim u As Integer, n As Integer
Dim riga As Integer, g_min As Integer, g_max As Integer
Dim c As Integer, gio As Integer, n_mese As Integer, a As Integer, m As Integer
Dim cas(), num As Variant, t As Boolean, succ As Boolean
Dim resto As Integer, r As Integer
Application.ScreenUpdating = False
Randomize Timer
caselle = [B2]
mesi = [B3]
mese = [C8]
anno = 2012
riga = 13
m = Int(caselle / mesi)
resto = caselle Mod mesi
With [A13].CurrentRegion
.ClearContents
.Interior.ColorIndex = 0
End With
For rip = 0 To mesi - 1
n_mese = mese + rip
If n_mese > 12 Then
n_mese = 1 + a
a = a + 1
If succ = False Then anno = anno + 1: succ = True
End If
u = Day(DateSerial(anno, n_mese + 1, 0))
g_min = 1
g_max = u
r = 0
If resto > 0 Then
r = 1
resto = resto - 1
End If
ReDim cas(m + r)
For n = 1 To m + r
c = c + 1
Do
gio = Int(Rnd() * (g_max - g_min + 1)) + g_min
t = False
For Each num In cas
If gio = num Then t = True: Exit For
Next
Loop Until t = False
cas(n) = gio
Cells(c + 12, 1) = riga - 12
Cells(riga, 2) = CDate(gio & "/" & n_mese & "/" & anno)
If rip Mod 2 = 0 Then
Cells(riga, 2).Interior.ColorIndex = 20
Else
Cells(riga, 2).Interior.ColorIndex = 36
End If
riga = riga + 1
Next n
Next
With [A13].CurrentRegion.Cells
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlCenter
End With
[B13:B50].Sort key1:=[B13], Header:=xlNo
Application.ScreenUpdating = True
End Sub
|
Function genera_lista_from_to(min As Long, max As Long)
Dim lista() As Long, i As Integer, r1 As Long, r2 As Long, tmp As Long
'genera una lista di numeri casuali non ripetuti nel range specificato
'esempio: u = genera_lista(1, 10): For Each k In u: Print k;: Next
Randomize Timer
ReDim lista(0 To (max - min)) As Long
'genera la lista di numeri consecutivi tra min e max
For i = min To max
lista(i - min) = i
Next
'quindi la disordina
For i = 1 To 10000
r1 = Rnd * UBound(lista)
r2 = Rnd * UBound(lista)
'swap
tmp = lista(r1)
lista(r1) = lista(r2)
lista(r2) = tmp
Next
genera_lista_from_to = lista()
End Function
Function estrai(lista, num_estrazioni) As Variant
Dim i As Integer, k() As Long
'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
If num_estrazioni > UBound(lista) + 1 Or num_estrazioni <= 0 Then
estrai = Array(False)
Exit Function
End If
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
|
