1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
Option Explicit
Dim nbmodif As Integer
Dim nbpassage As Integer
Sub complete()
Dim boucle As Variant
Range("a1:i9").Select
For Each boucle In Selection
If IsEmpty(boucle) Then boucle.Formula = "123456789"
Next boucle
End Sub
Sub evanbcar()
Dim tot As Integer
Dim x As Variant
Worksheets("sudo").Range("a1:i9").Select
For Each x In Selection
tot = tot + 9 - Len(x.Value)
Next x
Worksheets("sudo").Cells(2, 10).Select
Selection.Value = (9 * 9 * 9) - tot
End Sub
Sub modcarre(carre As Integer, valeur As Variant)
Dim ligne As Integer
Dim colonne As Integer
Dim carcours As Integer
For ligne = 1 To 9
For colonne = 1 To 9
carcours = (Int((colonne - 1) / 3) + 1) + ((Int((ligne - 1) / 3)) * 3)
If carcours = carre Then
Worksheets("sudo").Cells(ligne, colonne).Select
If Len(Selection) <> 1 Then
Selection.Formula = raye(Selection.Value, valeur)
End If
End If
Next colonne
Next ligne
End Sub
Sub unicol()
Dim nb As Variant
Dim colonne As Integer
Dim ligne As Integer
Dim occur As Integer
For nb = 1 To 9
For colonne = 1 To 9
occur = 0
For ligne = 1 To 9
Worksheets("sudo").Cells(ligne, colonne).Select
If Selection.Value = nb Then
occur = 0
Exit For
End If
If InStr(1, Selection.Value, nb, 1) <> 0 Then occur = occur + 1
Next ligne
If occur = 1 Then
For ligne = 1 To 9
Worksheets("sudo").Cells(ligne, colonne).Select
If InStr(1, Selection.Value, nb, 1) <> 0 Then
Selection.Value = nb
nbmodif = nbmodif + 1
End If
Next ligne
End If
evanbcar
Next colonne
Next nb
End Sub
Sub uniligne()
Dim nb As Variant
Dim colonne As Integer
Dim ligne As Integer
Dim occur As Integer
For nb = 1 To 9
For ligne = 1 To 9
occur = 0
For colonne = 1 To 9
Worksheets("sudo").Cells(ligne, colonne).Select
If Selection.Value = nb Then
occur = 0
Exit For
End If
If InStr(1, Selection.Value, nb, 1) <> 0 Then occur = occur + 1
Next colonne
If occur = 1 Then
For colonne = 1 To 9
Worksheets("sudo").Cells(ligne, colonne).Select
If Len(Selection.Value) <> 1 And InStr(1, Selection.Value, nb, 1) <> 0 Then
Selection.Value = nb
nbmodif = nbmodif + 1
End If
Next colonne
End If
Next ligne
Next nb
End Sub
Sub unicarr()
Dim enc As Variant
Dim nb As Variant
Dim colonne As Integer
Dim ligne As Integer
Dim occur As Integer
For ligne = 1 To 7 Step 3
For colonne = 1 To 7 Step 3
For nb = 1 To 9
occur = 0
Worksheets("sudo").Range(Cells(ligne, colonne), Cells(ligne + 2, colonne + 2)).Select
For Each enc In Selection
If enc.Value = nb Then
occur = 0
Exit For
End If
If InStr(1, enc.Value, nb, 1) <> 0 Then occur = occur + 1
Next enc
If occur = 1 Then
For Each enc In Selection
If InStr(1, enc.Value, nb, 1) <> 0 Then
enc.Value = nb
nbmodif = nbmodif + 1
End If
Next enc
End If
Next nb
evanbcar
Next colonne
Next ligne
End Sub
'
'
Sub verifb()
Dim ligne As Integer
Dim colonne As Integer
Dim valeur As Variant
Dim carre As Integer
nbpassage = nbpassage + 1
nbmodif = 0
Call unicol
Call uniligne
Call unicarr
For ligne = 1 To 9
For colonne = 1 To 9
Worksheets("sudo").Cells(ligne, colonne).Select
valeur = Selection.Value
If Len(valeur) = 1 Then
carre = (Int((colonne - 1) / 3) + 1) + ((Int((ligne - 1) / 3)) * 3)
Call modligne(ligne, valeur)
Call modcol(colonne, valeur)
Call modcarre(carre, valeur)
evanbcar
End If
Next colonne
Next ligne
If nbmodif <> 0 Then
MsgBox ("modifications à ce passage: " & nbmodif)
Call verifb
Else
MsgBox ("solution trouvée en " & nbpassage)
End If
End Sub
Sub modligne(ligne As Integer, valeur)
Dim colonne As Integer
For colonne = 1 To 9
Worksheets("sudo").Cells(ligne, colonne).Select
If Len(Selection) <> 1 Then
Selection.Formula = raye(Selection.Value, valeur)
End If
Next colonne
End Sub
Sub modcol(colonne As Integer, valeur)
Dim ligne As Integer
For ligne = 1 To 9
Worksheets("sudo").Cells(ligne, colonne).Select
If Len(Selection) <> 1 Then
Selection.Formula = raye(Selection.Value, valeur)
End If
Next ligne
End Sub
Function raye(dans As String, quoi As Variant) As Variant
Dim posi As Integer
posi = InStr(1, dans, quoi)
If posi <> 0 Then
raye = Left(dans, posi - 1) & Right(dans, Len(dans) - posi)
nbmodif = nbmodif + 1
Else
raye = dans
End If
End Function |
Partager