CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: [RESOLVED] Help with Sudoku code

1. Junior Member Join Date
Nov 2013
Posts
1

## [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  Reply With Quote

2. Elite Member Power Poster           Join Date
Nov 2003
Location
Florida
Posts
12,635

## Re: Help with Sudoku code 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?  Reply With Quote

excel, game #### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•