How to make a sudoku in Excel

owais87

New Member
Joined
May 15, 2024
Messages
1
Office Version
  1. Prefer Not To Say
Hello can someone please tell me how can I make sudoku in Excel through VBA I mean I'm facing some problems in the codes I have I'll write down the codes below please do tell me what's wrong with them or if anyone can give me new ones that'll much better. And I mean that should have auto generated button etc
These are the codes i tried :
Dim SolutionGrid(1 To 9, 1 To 9) As Integer

Sub GeneratePuzzle()
Dim i As Integer, j As Integer

' Clear existing puzzle
For i = 1 To 9
For j = 1 To 9
Cells(i, j).Value = ""
Next j
Next i

' Generate new puzzle (solved)
GenerateSolution
' Display the puzzle
DisplayPuzzle
End Sub

Sub NewPuzzle()
' Clear existing puzzle
For Each cell In Range("A1:I9")
cell.Value = ""
Next cell
' Generate and display new puzzle
GenerateSolution
DisplayPuzzle
End Sub

Sub GenerateSolution()
' Generate a solved Sudoku puzzle
' For simplicity, let's assume we have a solved puzzle
' This is where you would implement your Sudoku solving algorithm or use a pre-solved grid
' For demonstration, let's use a predefined solution grid
Dim i As Integer, j As Integer
For i = 1 To 9
For j = 1 To 9
SolutionGrid(i, j) = (i + j) Mod 9 + 1
Next j
Next i
End Sub

Sub DisplayPuzzle()
' Copy the solution grid to the puzzle grid
Dim i As Integer, j As Integer
For i = 1 To 9
For j = 1 To 9
If Int(Rnd * 2) = 0 Then ' Randomly remove some numbers
Cells(i, j).Value = SolutionGrid(i, j)
Else
Cells(i, j).Value = ""
End If
Next j
Next i
End Sub
If anyone knows anything please do tell me
Thank-you
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
@owais87 Welcome.
I don't do Sudoku but was curious to have a little look at this.
I wonder if the below hybrid of vba and excel might be of value?

Use the Copy icon in the top left corner of the attached XL2BB mini sheet and Paste it into B5 of a blank sheet.
Then there are two bits of vba which if they prove to work for you can be attached to sheet buttons or whatever.
One code will create a new puzzle solution and grid.
The other will create another puzzle grid for the existing solution. You can enter different values in F24 to give some degree of variation in the number of visible numbers in the grid.

Testing Formula Update.xlsm
BCDEFGHIJKLMNOPQRSTUVWXYZ
5Helpers
6Root#123456789
7Rand0.50.50.30.20.90.50.911
8Rand#658937214
93
10Root PaternSOLUTION :Numbers RandomisedCheck
1112345678965893721445
1245678912393721465845
1378912345621465893745
1491234567846589372145
1534567891289372146545
1667891234572146589345
1723456789158937214645
1856789123437214658945
1989123456714658937245
20
21Check454545454545454545
22
23
24Blank Factor 5
25
26
27Puzzle Grid
28
29974
301593
313
3296
33893
34537
352
3667
37
Sheet1
Cell Formulas
RangeFormula
C8:K8C8=INDEX($C6:$K6,RANK(C7,$C7:$K7))
P11:X19P11=INDEX(C$8:K$8,C11:K19)
Z11:Z19Z11=SUM(P11:X11)
P21:X21P21=SUM(P11:P19)
Dynamic array formulas.


Hope that helps?
 
Upvote 0
Upvote 0
Forgot to post the vba :oops:
VBA Code:
Sub NewPuzzle()
Dim i As Integer, j As Integer
Dim SolRng As Range, GridRng As Range
'randomise
Range("C7:K7").Formula = "=RAND()"
'Fix
Range("C7:K7").Value = Range("C7:K7").Value

Set SolRng = Range("P11:X19")
Set GridRng = Range("C28:K36")

With GridRng
    .ClearContents
    For i = 1 To 9
        For j = 1 To 9
         ' Randomise visible numbers based on Number (2-7) entered in F24
        If Int(Rnd * Range("F24")) = 0 Then .Cells(i, j) = SolRng.Cells(i, j)
        Next j
    Next i
End With

End Sub

VBA Code:
Sub NewGridOnly()
Dim i As Integer, j As Integer
Dim SolRng As Range, GridRng As Range
Set SolRng = Range("P11:X19")
Set GridRng = Range("C28:K36")
 
With GridRng
    .ClearContents
    For i = 1 To 9
        For j = 1 To 9
         ' Randomise visible numbers based on Number (2-7) entered in F24
        If Int(Rnd * Range("F24")) = 0 Then .Cells(i, j) = SolRng.Cells(i, j)
        Next j
    Next i
End With

End Sub
Time for a lie down...
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,141
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top