How to Generate 3x3 Magic Square Permutations in Excel VBA
Question details
The user wants to write a VBA macro that generates all possible 3x3 magic squares using the digits 1 through 9, ensuring all rows, columns, and diagonals sum to exactly 15.

- Product
- Excel / WPS Spreadsheet
- Device & OS
- not provided
- Scenario
- Creating a mathematical puzzle or permutation generator using VBA scripting.
- Observed behavior
- The user needs an efficient VBA algorithm that generates permutations and uses incremental validation to keep only valid 3x3 arrangements without duplicate outputs.
Ensure that the Developer tab is enabled in your spreadsheet application so you can access the VBA Editor. It is highly recommended to save your workbook as a Macro-Enabled Workbook (.xlsm) before running intensive permutation scripts.
Use Recursive Permutation with Incremental Validation
Improve performance by building the square row-by-row and rejecting invalid partial squares early before completing the permutation.
Generating all possible 9-digit permutations creates 362,880 combinations. Testing the sums only after all nine positions are filled is computationally expensive. Instead, you can build the square incrementally and apply validation tests as soon as enough values are available.
By validating a row immediately after its three digits are generated, you can skip calculating the rest of the square if the sum does not equal 15, significantly speeding up the macro execution.
Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor in your spreadsheet.
Click 'Insert' from the top menu, then select 'Module' to create a blank script window.
Define a recursive subroutine that loops through the digits 1 to 9. Use an array to track unused digits so numbers are not reused.
Add an IF condition right after the 3rd, 6th, and 9th digits are placed. Check if the sum of the most recent row equals 15. If it does not, exit that branch of the recursion early.
Once the script confirms all rows, columns, and diagonals equal 15, write the surviving 3x3 array directly to the active worksheet using the Cells() property.

Generate Permutations with Post-Processing
Generate all 9-digit permutations recursively and test the sums once the square is fully populated.
Run Your VBA Macros Flawlessly with WPS Office
WPS Spreadsheet offers excellent compatibility with Excel VBA macros, allowing you to run complex algorithms like recursive permutations natively without rewriting your code.
- 1. Open Your Spreadsheet: Launch WPS Office and open your .xlsm or .xlsx file.
- 2. Access the Developer Tab: Navigate to the 'Developer' tab on the top ribbon.
- 3. Open the VBA Editor: Click 'Visual Basic' to open the familiar VBA scripting environment.
- 4. Run the Macro: Paste your 3x3 Magic Square generator code into a module and click 'Run'.

Frequently Asked Questions
Why does my VBA permutation macro run so slowly?
Generating all 9-digit permutations creates over 360,000 combinations. Checking them all at the end is inefficient. Using incremental validation to discard invalid rows (those not summing to 15) early significantly speeds up the macro by drastically reducing the number of calculations.
How do I output the generated magic squares to the worksheet?
In your VBA script, use the `Cells(row, column).Value` property to write the validated 3x3 array into the active worksheet. Make sure to increment a row offset variable for each new magic square found so they do not overwrite each other.
Do Microsoft Excel macros work natively in WPS Office?
Yes, WPS Spreadsheet features robust built-in support for VBA and macros. You can open your existing .xlsm files and execute your custom VBA code directly in WPS Office without needing complex modifications.




