Vba Nombre aleatoire entre 0 et 10

  • Initiateur de la discussion carlos
  • Date de début
C

carlos

Guest
Bonjour

G le message suivant sur cette macro : erreur 438

Sub aleatoire()
Dim cell As Range
For Each cell In Range('a2:a11')
If cell = Empty Then cell = Application.WorksheetFunction.rand()
Next
End Sub

En faite je souhaite quand cell est vide coller un nombre aleatoire compris entre 0 et 10

Merci
 

_Thierry

XLDnaute Barbatruc
Repose en paix
Bonjour Carlo,

Tu n'as pas cherché à 'Aléatoire' dans ce Forum ni dans la 'Première Génération'...

Pas besoin de la WorkSheetFunction ici, un Randomize Statement est compris par VBA...

Sub Aleatoire()
Dim cell As Range
For Each cell In Range('a2:a10')
Randomize
If cell = Empty Then cell = Int(11 * Rnd) 'Generate random value between 0 and 10
Next
End Sub

Bon Aprèm
@+Thierry


EDITION !!!
Bonjour à Karma Killer et à Péchinet, sorry j'avais pas raffraichi !!!

Message édité par: _Thierry, à: 29/03/2005 16:48
 

pat1545.

XLDnaute Accro
Salut,

trouvé sur le et adapté à mes besoins pour le lotto belge:

à toi d'adapter

Patrick

' 2 eme solution
'Places numbers in A1:A6

Sub TheBestLuckyLottoPicker()
Dim t As Integer, m As Integer, b As Integer
Range('A2').Select
ActiveCell.Offset(0, 0).Rows('1:6').EntireRow.Select
Selection.Insert Shift:=xlDown
Do While b < 6
k = 6: n = 42
t = 0: m = 0
Do While m < k
Randomize
If (n - t) * Rnd() < k - m Then
m = m + 1
Cells(m, b + 1) = t + 1
End If
t = t + 1
Loop
b = b + 1
Loop 'b
Range('A1').Select
ActiveCell.Offset(0, 0).Rows('1:2').EntireRow.Select
Selection.Insert Shift:=xlDown
ActiveSheet.Range('F1') = 'BONNE CHANCE'
ActiveSheet.Range('A1') = 'TIRAGE ALEATOIRE POUR LE PROCHAIN LOTTO'
Range('A1').Select
End Sub
 

Discussions similaires

Réponses
1
Affichages
235
Réponses
0
Affichages
208

Statistiques des forums

Discussions
312 980
Messages
2 094 124
Membres
105 941
dernier inscrit
antho_qh