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
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