# [RESOLVED] Help with Sudoku code

• November 7th, 2013, 06:08 PM
kk123
[RESOLVED] Help with Sudoku code
Hi, I'm coding a sudoku generator in excel for fun and the part of the code that generates the board crashes excel every time I execute. Any idea what I'm doing wrong? I don't think I'm sending it into an infinite loop but I've never used While statements before so I could have overlooked something.

Sub Gen()

Dim x As Integer
Dim i As Integer, j As Integer, k As Integer
Dim M(1 To 9, 1 To 9, 1 To 9) As Integer
Dim r As Integer, c As Integer, b As Integer
Dim Match As Boolean

'reset M
For r = 1 To 9
For c = 1 To 9
For b = 1 To 9
M(r, c, b) = 0
Next
Next
Next

For r = 1 To 9
For c = 1 To 9
b = solveb(r, c)
'solveb returns an integer for which box r*c falls into; code there is accurate/has been tested successfully

Match = False

While (Match = False) 'tests to see if a number (x, which is random 1-9) is already used in the same row, column, or box

x = Int((9 * Rnd) + 1)

For i = 1 To 9
If M(i, c, b) = x Then Match = True
Next

For j = 1 To 9
If M(r, j, b) = x Then Match = True
Next

For k = 1 To 9
If M(r, c, k) = x Then Match = True
Next

Wend

M(r, c, b) = x

Next
Next

'display

For r = 1 To 9
For c = 1 To 9
b = solveb(r, c)

Worksheets("Sheet1").Cells(r + 5, c + 5).Value = M(r, c, b)

Next
Next

End Sub
• November 7th, 2013, 06:14 PM
GCDEF
Re: Help with Sudoku code
Quote:

Originally Posted by kk123
Hi, I'm coding a sudoku generator in excel for fun and the part of the code that generates the board crashes excel every time I execute. Any idea what I'm doing wrong?

Posting Basic code in a C++ forum?