Question sur mon code
bonsoir , j'ai saisi le bout de code suivant :
Option Base 1
Sub Feuil4_Bouton1_QuandClic()
Dim t As Variant
For i = 1 To 9
For j = 1 To 9
For k = 1 To 9
If i <> j And i <> k And j <> k Then
If i + j + k = 15 Then
MsgBox i & " " & j & " " & k
GoTo 2
End If
End If
Next
Next
Next
2:
'deuxieme partie :
t = Array(i, j, k)
For u = 1 To 9
For v = 1 To 9
For w = 1 To 9
For s = 1 To UBound(t)
If u <> t(s) And v <> t(s) And w <> t(s) And u <> v And u <> w And v <> w And (u + v + w = 15) Then
MsgBox u & " " & v & " " & w
GoTo 3
End If
Next
Next
Next
Next
3: MsgBox "fin"
End Sub
la premiere partie du code me genere un triplet (i,j;k) avec les premieres contraintes , pas de soucis
la seconde partie du code doit me generer un triplet (u,v,w) different du premier , donc j'ai realisé un tableau pour le premier triplet
ou chaque element de celui ci ne doit pas etre repris pour generer le second triplet, le soucis et que j'obtiens "1 5 9" pour i,j,k
et "1,6,8 " pour u ,v ,w , ici la valeur 1 est reprise alors qu'elle n'aurait pas du l'etre.
Je ne vois donc pas ce qui cloche dans les lignes , surtout que la condition u <> t(s) empeche que u prenne des valeurs dans t .
Verriez vous ou se trouve l'anomalie ?
En vous remerciant par avance
- Messages
- 1'794
- Excel
- 2010
- Inscrit
- 25/08/2014
- Emploi
- Consultant VB6 / SQL / VBA / Excel / Access
Bonsoir,
2 points :
- pour la deuxième partie, le parcours du tableau doit commencer à 0 (à la place de 1)
- selon l'algo, on affiche le message après avoir testé uniquement sur un indice donné. Le test est à l'intérieur d'une boucle, donc on ne vérifie pas toute la boucle (les 3 indices) avant d'afficher le résultat.
Si je comprends bien le résultat attendu (le deuxième test doit exclure les 3 valeurs trouvées lors du premier passage), une nouvelle version en PJ, à tester.
Bonne soirée
Bouben
Ci-dessous le code revu :
Sub Feuil4_Bouton1_QuandClic()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim u As Integer
Dim v As Integer
Dim w As Integer
Dim s As Integer
Dim t As Variant
Dim bOK As Boolean
For i = 1 To 9
For j = 1 To 9
For k = 1 To 9
If i <> j And i <> k And j <> k Then
If i + j + k = 15 Then
MsgBox i & " " & j & " " & k
GoTo 2
End If
End If
Next
Next
Next
2:
'deuxieme partie :
t = Array(i, j, k)
For u = 1 To 9
For v = 1 To 9
For w = 1 To 9
'V0.1
'' For s = 1 To UBound(t)
'' If u <> t(s) And v <> t(s) And w <> t(s) And u <> v And u <> w And v <> w And (u + v + w = 15) Then
'' MsgBox u & " " & v & " " & w
'' GoTo 3
'' End If
If u <> v And u <> w And v <> w And (u + v + w = 15) And (u + v + w = 15) Then
bOK = True
Else
bOK = False
End If
If bOK Then
For s = 0 To UBound(t) 'commencer à 0
If u = t(s) Or v = t(s) Or w = t(s) Then
bOK = False
Exit For
End If
Next s
End If
If bOK Then
MsgBox u & " " & v & " " & w
End If
'V0.1-fin
Next
Next
Next
3: MsgBox "fin"
End Sub
Bonsoir...
Trop tard...
Option Base 1
Sub Feuil4_Bouton1_QuandClic()
Dim t As Variant
For i = 1 To 9
For j = 1 To 9
For k = 1 To 9
If i <> j And i <> k And j <> k Then
If i + j + k = 15 Then
MsgBox i & " " & j & " " & k
GoTo 2
End If
End If
Next
Next
Next
2:
For u = 1 To 9
If u = i Or u = j Or i = k Then
Else
For v = 1 To 9
If v = i Or v = j Or v = k Or v = u Then
Else
For w = 1 To 9
If w = i Or w = j Or w = k Or w = u Or w = v Then
Else
If u + v + w = 15 Then
MsgBox u & " " & v & " " & w
GoTo 3
End If
End If
Next
End If
Next
End If
Next
3: MsgBox "fin"
End Sub
@ binetôt
LouReeD
Je vous remercie infiniment pour vos réponses , je vais examiner cela
bonjour
en reflechissant a tout ca, j'ai trouvé une autre solution peut etre pas elegante par sa longueur mais qui marche:
Sub Feuil4_Bouton1_QuandClic()
Dim f, g, h As Variant
For i = 1 To 9
For j = 1 To 9
For k = 1 To 9
If i <> j And i <> k And j <> k Then
If i + j + k = 15 Then
MsgBox i & " " & j & " " & k
GoTo 2
End If
End If
Next
Next
Next
2:
'deuxieme partie :
f = Array(i, j, k)
g = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
For m = 0 To UBound(g)
For n = 0 To UBound(f)
If g(m) = f(n) Then
g(m) = Replace(g(m), f(n), "x")
End If
Next
Next
i = 0
j = 0
k = 0
For i = 0 To UBound(g)
For j = 0 To UBound(g)
For k = 0 To UBound(g)
If IsNumeric(g(i)) Then
If IsNumeric(g(j)) Then
If IsNumeric(g(k)) Then
If g(i) <> g(j) And g(i) <> g(k) And g(j) <> g(k) And g(i) + g(j) + g(k) = 15 Then
MsgBox g(i) & " " & g(j) & " " & g(k)
End If
End If
End If
End If
Next
Next
Next
End Sub