Yes—Excel can solve Sudoku. For a practical solver, enter the puzzle in cells B2:J10 and run a short VBA macro that checks possible digits until it finds a solution; Excel’s built-in Solver add-in is another option, but it is usually less convenient for a standard 9-by-9 puzzle.
What an Excel Sudoku Solver Does
A Sudoku solver takes a partially filled 9-by-9 grid and fills its empty cells while following the puzzle’s rules: each row, each column, and each 3-by-3 box must contain the digits 1 through 9 exactly once. The clues already in the grid are fixed; a correct solver does not change them.
Excel is a spreadsheet rather than a dedicated Sudoku application, so it does not include a ready-made Sudoku-solving command. You can use it as a grid for entering clues, apply formulas or formatting to help check a puzzle, or write a macro that searches for a solution. For most people who want to solve a puzzle inside a workbook, a VBA macro is the most direct approach.
The macro below uses backtracking: it tries a legal digit in an empty cell, continues to another empty cell, and reverses an earlier choice if it reaches a contradiction. It returns the first solution it finds. That distinction matters if a puzzle has more than one valid completion: the macro will solve it, but it will not tell you whether the solution is unique.
Set Up the Sudoku Grid
Use a blank worksheet, or choose a clear 9-by-9 area on an existing sheet. This example expects the top-left cell of the puzzle to be B2 and the bottom-right cell to be J10. Enter the given digits in those cells and leave all unsolved cells empty. Do not enter zeroes as placeholders; the macro treats blank cells as the positions to solve.
- Enter each clue as a number from 1 to 9 in its corresponding cell.
- Leave every unknown cell blank. For example, a clue in the puzzle’s top-left position goes in B2, while the cell one column to its right goes in C2.
- Save the workbook as an Excel Macro-Enabled Workbook (
.xlsm) so it can retain VBA code. - Open the Visual Basic Editor and insert a standard module. On Windows, press Alt+F11, then choose Insert > Module. If the Developer tab is hidden, enable it through File > Options > Customize Ribbon.
- Paste the code below into the module, return to the worksheet, and run
SolveSudokufrom Developer > Macros.
On Mac, VBA is available in desktop Excel, though keyboard shortcuts can depend on the keyboard and function-key settings. You can open the editor from Developer > Visual Basic; if the Developer tab is hidden, enable it in Excel > Preferences > Ribbon & Toolbar. Excel for the web can store a macro-enabled workbook but cannot run VBA macros, so open the file in the desktop application to use this solver.
VBA Backtracking Solver
This macro reads B2:J10, checks that entries are whole numbers from 1 to 9, rejects conflicting clues, and then searches for a completion. It writes the completed grid back to the same cells. It does not require an add-in or a worksheet formula.
Option Explicit
Public Sub SolveSudoku()
Dim ws As Worksheet
Dim board(1 To 9, 1 To 9) As Long
Dim r As Long, c As Long
Dim v As Variant
Dim valueNum As Double
Set ws = ActiveSheet
For r = 1 To 9
For c = 1 To 9
v = ws.Cells(r + 1, c + 1).Value2
If IsError(v) Then
MsgBox "The puzzle grid contains an error value.", vbExclamation
Exit Sub
End If
If Len(Trim$(CStr(v))) = 0 Then
board(r, c) = 0
ElseIf Not IsNumeric(v) Then
MsgBox "Enter only whole numbers from 1 to 9, or leave a cell blank.", vbExclamation
Exit Sub
Else
valueNum = CDbl(v)
If valueNum < 1 Or valueNum > 9 Or valueNum <> Fix(valueNum) Then
MsgBox "Enter only whole numbers from 1 to 9, or leave a cell blank.", vbExclamation
Exit Sub
End If
board(r, c) = CLng(valueNum)
End If
Next c
Next r
For r = 1 To 9
For c = 1 To 9
If board(r, c) <> 0 Then
If Not CanPlace(board, r, c, board(r, c)) Then
MsgBox "The given clues conflict at grid row " & r & _
", column " & c & ".", vbExclamation
Exit Sub
End If
End If
Next c
Next r
If Not SolveBoard(board) Then
MsgBox "No solution was found for these clues.", vbInformation
Exit Sub
End If
For r = 1 To 9
For c = 1 To 9
ws.Cells(r + 1, c + 1).Value = board(r, c)
Next c
Next r
MsgBox "Sudoku solved.", vbInformation
End Sub
Private Function SolveBoard(ByRef board() As Long) As Boolean
Dim r As Long, c As Long, n As Long
For r = 1 To 9
For c = 1 To 9
If board(r, c) = 0 Then
For n = 1 To 9
If CanPlace(board, r, c, n) Then
board(r, c) = n
If SolveBoard(board) Then
SolveBoard = True
Exit Function
End If
board(r, c) = 0
End If
Next n
SolveBoard = False
Exit Function
End If
Next c
Next r
SolveBoard = True
End Function
Private Function CanPlace(ByRef board() As Long, ByVal r As Long, _
ByVal c As Long, ByVal n As Long) As Boolean
Dim i As Long, boxRow As Long, boxCol As Long
Dim rr As Long, cc As Long
For i = 1 To 9
If i <> c And board(r, i) = n Then Exit Function
If i <> r And board(i, c) = n Then Exit Function
Next i
boxRow = ((r - 1) \ 3) * 3 + 1
boxCol = ((c - 1) \ 3) * 3 + 1
For rr = boxRow To boxRow + 2
For cc = boxCol To boxCol + 2
If (rr <> r Or cc <> c) And board(rr, cc) = n Then Exit Function
Next cc
Next rr
CanPlace = True
End Function
The code is written for a standard module in desktop Excel VBA. The backslash in the box-start calculations is VBA’s integer-division operator. The code checks the row, column, and box before placing a digit; if a later choice makes completion impossible, it resets that cell and tries another digit. The search can be slower on a difficult puzzle than a more advanced solver, but a 9-by-9 grid has a small enough search space for this straightforward method to be useful.
Run It Safely and Understand the Result
Before running a macro, save a copy of the workbook. Macro actions cannot be reversed with Ctrl+Z, and this macro replaces the contents of the puzzle range with the solved values. The original clues remain the same, but keeping an untouched copy makes it easy to start again or compare the result.
Make sure the intended worksheet is active before running the macro: it reads the active sheet, not a worksheet identified by name. A useful precaution is to put the puzzle on its own sheet and click that sheet before choosing Developer > Macros. If the macro says that clues conflict, check for a repeated digit in a row, column, or 3-by-3 box, as well as accidental text or a number outside the range 1 to 9.
When Excel displays a security warning, only enable macros in a workbook whose code and source you trust. A macro-enabled file is not automatically unsafe, but VBA can make changes to a workbook and, in some cases, perform other actions. The code shown here operates on the active sheet’s specified cells; saving a separate copy is still a sensible precaution.
Using Excel Solver Instead
Excel’s Solver add-in is an optimization tool, not a Sudoku feature. In principle, a Sudoku can be represented as a model with a binary decision variable for every possible digit in every cell, plus constraints requiring one digit per cell and one occurrence of each digit in each row, column, and box. Clue cells must also be fixed to their supplied values.
That formulation involves 729 possible cell-and-digit choices before constraints are considered. The standard Solver add-in has a limit of 200 variable cells, so this full binary-variable model does not fit the standard add-in’s ordinary changing-cell limit. Building a smaller or more specialized optimization model is possible in principle, but it is not the easiest route for a typical worksheet user. A backtracking macro avoids the need to create and maintain a large Solver model.
If you still want to explore Solver, it may need to be enabled first: on Windows, go to File > Options > Add-ins, choose Excel Add-ins in the Manage box, click Go, and select Solver Add-in. When enabled, Solver is available from Data > Solver. The exact controls on Mac can differ, and add-in availability depends on the Excel installation. Enabling Solver alone does not create a Sudoku model or make the macro above unnecessary.
Worksheet Checks and Visual Improvements
Formatting can make the grid easier to read without changing how the solver works. Apply borders around each 3-by-3 box, center the digits, and make the nine puzzle columns similar in width. You can also use a different font color for original clues before running the macro, although the supplied code does not preserve a separate list of clue cells or apply special formatting to its answers.
For a manual error check, inspect every completed row, column, and box for the digits 1 through 9 with no repeats or omissions. The macro’s placement rules already enforce those conditions, so a successfully completed board should satisfy them. If you build your own worksheet checks with formulas, remember that formatting a cell to look blank is not the same as leaving it empty: this macro treats a cell containing a value as an entry and will validate it.
Limitations and Common Problems
A solver can only complete a puzzle consistent with its clues. If a copied clue is wrong, the macro may report no solution even though the printed puzzle itself is solvable. Check the original grid carefully, especially similar-looking digits, and confirm that each clue was entered in the correct row and column. The macro checks for conflicts among the clues, but it cannot know which clue was mistyped.
- Nothing happens: Confirm that you are using desktop Excel, that the workbook is saved as
.xlsm, and that macros are permitted for this trusted file. - The result is in the wrong place: The macro expects B2:J10 on the active sheet. Move the puzzle there or change the cell references in the code.
- The macro reports conflicting clues: Look for duplicates in a row, column, or box, and remove any accidental entries.
- The macro reports no solution: Check the clue transcription. A valid but unusually challenging puzzle should still be solvable by this search method, though an advanced solver can be faster.
- You need to keep the starting puzzle: Save a copy before running, or duplicate the worksheet and run the macro on the duplicate.
Another common misconception is that every well-formed Sudoku has exactly one solution. Many published puzzles are designed to have a unique solution, but a grid of clues can also admit multiple valid completions. This macro stops at the first one it finds. To test uniqueness, the code would need to continue searching after the first completion and count solutions, stopping once it finds more than one.
Alternatives to a VBA Solver
If you do not want to enable macros, a dedicated Sudoku website or app is usually the simplest alternative: enter the clues and let its solver return a completion. Check that the service accepts a standard 9-by-9 grid and explains whether it detects invalid clues or multiple solutions. If the puzzle is private or you prefer not to upload it, use an offline app or solve it locally in Excel.
You can also solve a puzzle manually with pencil marks or candidate lists. Excel can serve as a convenient digital grid, but maintaining candidate sets in a worksheet takes more setup than using a purpose-built Sudoku app. For learning, a solver that explains techniques such as singles, pairs, and box-line interactions can be more informative than a macro that simply returns a completed grid. For checking a finished puzzle, compare each row, column, and box against the digits 1 through 9.
Frequently Asked Questions
Can Excel solve Sudoku without VBA?
Yes, but it takes more setup. You can model the puzzle with worksheet calculations or use Solver where a suitable model fits its limits. For a typical 9-by-9 puzzle, a VBA backtracking macro is generally more straightforward than building a formula-based solver.
Does the macro work in Excel for the web?
No. Excel for the web does not run VBA macros. Open the macro-enabled workbook in desktop Excel for Windows or Mac to run the code.
Where do I enter the Sudoku clues?
This example uses B2:J10, with the puzzle’s top-left cell in B2. Put the given digits in their matching cells and leave unknown cells blank. If you use a different range, update the worksheet references in the macro.
Can this solver show whether a puzzle has multiple answers?
No. It returns the first valid completion it finds and stops. Detecting multiple solutions requires adapting the search to keep counting completions instead of exiting at the first one.
Why does Excel say there is no solution?
The entered clues may contain a typo, may conflict within a row, column, or 3-by-3 box, or may not match the intended puzzle. Check the grid and confirm that all clues are whole numbers from 1 to 9.
Related
- Import CSV Into Excel Without Losing Leading Zeros
- Import CSV Into an Existing Excel Table: Steps & Tips
- Import Excel to Jira CSV: Sample File and Import Steps
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Macro Recorder Software: How It Works in ExcelMacro recorder software captures a sequence of actions and lets you replay them, reducing repetitive work. In Microsoft…
- Excel UserForm Events: What Fires, When, and Why in VBAExcel UserForm events are VBA procedures that run in response to a form’s life cycle or to actions…
- VBA Function That Returns an Array: Examples and TipsIn VBA, declare a function that returns an array as As Variant, then assign an array to the…
- Excel VBA Date Subtraction: Get the Number of DaysIn Excel VBA, subtract one date from another with days = endDate - startDate; for dates without times,…
- Excel Macro Commands List: Common VBA Actions ExplainedAn Excel macro command is a Visual Basic for Applications (VBA) instruction that tells Excel what to do,…
- Outlook VBA: Pull Email Data into ExcelTo pull email data from Outlook into Excel, run a VBA macro in Excel that connects to classic…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.