# Sudoku in VBA ( no functions ) Help Immediate

Page 1 of 1

## 3 Replies - 9222 Views - Last Post: 09 November 2006 - 04:23 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=20276&amp;s=a60703096c6a87b5405a0335f2581a4a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 czanime

Reputation: 0
• Posts: 2
• Joined: 30-October 06

# Sudoku in VBA ( no functions ) Help Immediate

Posted 30 October 2006 - 05:46 PM

'I need help with creating my Sudoku Solver code and I just did the first line to see if it work
'Well its not working now, and i need help. Im new in VBA and I just know basic looping, Reading and writing on worksheets and some other novince stuff.

Here is what i started so far. dont laugh.

Option Explicit
Sub SudokuProgram()
Dim col1(1 To 9) As Integer
Dim row1(1, 1 To 9) As Integer
Dim row As Integer
Dim col As Integer
Dim I As Integer 'Count
Dim sec1(1 To 3, 1 To 3) As Integer
Dim sec2(1 To 3, 4 To 6) As Integer
Dim sec3(1 To 3, 7 To 9) As Integer
Dim sec4(4 To 6, 1 To 3) As Integer
Dim sec5(4 To 6, 4 To 6) As Integer
Dim sec6(4 To 6, 7 To 9) As Integer
Dim sec7(7 To 9, 1 To 3) As Integer
Dim sec8(7 To 9, 1 To 3) As Integer
Dim sec9(7 To 9, 7 To 9) As Integer
Dim x As Integer 'number in a single cell
Dim Sudoku As Integer
row = 1
col = 1
x = (row = row + 1) 'x is any integer >= 9 in one cell
For I = 1 To 9
col1(I) = Worksheets("sheet1").Cells(I, 1).Value
If x = 1 Then
col1 Not 1
Else
If x = 2 Then
col Not 2
Else
If x = 3 Then
col Not 3
Else
If x = 4 Then
col Not 4
Else
If x = 5 Then
colNot 5
Else
If x = 6 Then
col Not 6
Else
If x = 7 Then
col Not 7
Else
If x = 8 Then
col Not 8
Else
If x = 9 Then
col Not 9
Else
If x = Sudoku(col) Then
MsgBox ("You cant use & x & twice in one colomn, please fix this.")
Range("x").Select
With Selection.Interior
.ColorIndex = 3
.Pattern = xlSolid
End With
End If
Next I
End Sub

So basically what im trying to do right now is just read one column with 9 cells to check for repeated numbers. If you dont know Sudoku, then thats usually the first step. If anybody can help me, give tips or send me there verson because its better and simpler i would greatly appreciate it. Thanks

Is This A Good Question/Topic? 0

## Replies To: Sudoku in VBA ( no functions ) Help Immediate

### #2 Trogdor

Reputation: 15
• Posts: 627
• Joined: 06-October 06

## Re: Sudoku in VBA ( no functions ) Help Immediate

Posted 01 November 2006 - 05:47 AM

So its not a solver but a checker, you check if the filled in number is legal.
Thats okay, if that is what you want.
You need to repeat the same code for rows and minigrids as well.

As for a sudoku solver, i would realy considder using another language for that, one that supports recursion and datatypes for example.

### #3 mathete

Reputation: 0
• Posts: 3
• Joined: 07-November 06

## Re: Sudoku in VBA ( no functions ) Help Immediate

Posted 09 November 2006 - 01:33 AM

I agree with above statement, and I dont mean to be rude, but with a little better nomenclature the code might be a wee bit easier to read.

### #4 KeyWiz

• D.I.C Regular

Reputation: 8
• Posts: 439
• Joined: 26-October 06

## Re: Sudoku in VBA ( no functions ) Help Immediate

Posted 09 November 2006 - 04:23 AM

There is a much better way to do you comparisons: try this instead of repeated if-then-else structures.
```Select Case x
Case = 1
col1 Not 1
Case = 2
col2 Not 2
Case = 3
col3 Not 3
Case = etc.
coletc. Not etc.
End Select

```

This is easier to read and follow and it executes faster by going diectly to the Case = Relevant State and skips all else with in the Select-Case instance.
You can also follow all your other Cases with Case Else to preform for unexpected results.

use the "code]" your code here "[/code " tags to keep your code programming formated, you can edit your original statement to add the tags before and after your code.

This post has been edited by KeyWiz: 09 November 2006 - 04:24 AM