Bonjour,
Une fonction personnalisée :
Function TIRAGE(n As Integer, m As Integer)
Dim tabt(), tablo() As Integer, i%, x%
Application.Volatile False
If m < 1 Or n < 1 Then
TIRAGE = CVErr(xlErrNum)
Exit Function
End If
ReDim tabt(1 To m)
If m > n Then
For i = n + 1 To m
tabt(i) = CVErr(xlErrNA)
Next i
m = n
End If
ReDim tablo(n)
For i = 1 To n
tablo(i) = i
Next i
Randomize
For i = 1 To n
x = Int(n * Rnd + 1)
tablo(0) = tablo(x)
tablo(x) = tablo(i)
tablo(i) = tablo(0)
Next i
tablo(0) = tablo(n)
x = Int(n * Rnd + 1)
For i = 1 To m
tabt(i) = tablo((x - 1 + i) Mod n)
Next i
If Selection.Rows.Count > 1 Then
TIRAGE = Application.Transpose(tabt)
Else
TIRAGE = tabt
End If
End Function
La fonction est matricielle (validation par Ctrl+Maj+Entrée)
Utilisation : exemple de ta question : sélectionner une plage de 4 cellules :
taper la fonction, avec en arguments le nb total d'éléments et le nombre d'éléments à tirer :
=TIRAGE(30;4)
La sélection peut être horizontale ou verticale.
La fonction ne se recalcule pas lorsque la feuille est recalculée (sinon ça pourrait changer sans arrêt).
Cordialement.