' Generates (Pan) Magic Squares of order 9 based on Sudoku Comparable Squares

' Tested with Office 2007 under Windows 7

```Sub CnstrSqrs9a()

Dim b(2, 81), a(81), c(81), s9(20)

y = MsgBox("Locked", vbCritical, "Routine CnstrSqrs9a")
End

n4 = 64     'Solutions961 :  64
'Solutions963 : 432
'Solutions964 : 144

n2 = 0: n9 = 0: k1 = 1: k2 = 1
s1 = 369

t1 = Timer

For j1 = 1 To n4

For j2 = 1 To n4
If j2 = j1 Then GoTo 20

j10 = j1: j20 = 1: GoSub 100                 'Read Sudoku Comparable Square 1
j10 = j2: j20 = 2: GoSub 100                 'Read Sudoku Comparable Square 2

For j4 = 1 To 81
a(j4) = b(1, j4) + 9 * b(2, j4) + 1
Next j4

GoSub 300: If fl1 = 0 Then GoTo 20           'Check identical numbers

n9 = n9 + 1: GoSub 740                       'Print results (selected numbers)
'       n9 = n9 + 1: GoSub 750                       'Print results (squares)

GoSub 400                                    'Construct Square c() (Squared Elements)

GoSub 500: If fl1 = 0 Then GoTo 20           'Check Magic Sum c()

n10 = n10 + 1: GoSub 745                     'Print results (squared numbers)

20  Next j2

Next j1

t2 = Timer

t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions for sum" + Str(s1)
y = MsgBox(t10, 0, "Routine CnstrSqrs9a")

End

'   Read Sudoku Comparable Squares (Line Format)

100 For i1 = 1 To 81
b(j20, i1) = Sheets("Input961").Cells(j10, i1).Value
Next i1
Return

'   Check identical numbers

300 fl1 = 1
For i1 = 1 To 81
a2 = a(i1)
For i2 = (1 + i1) To 81
If a2 = a(i2) Then fl1 = 0: Return
Next i2
Next i1
Return

'   Construct Square c() (Squared Elements)

400 For i1 = 1 To 81
c(i1) = a(i1) ^ 2
Next i1
Return

'   Check Magic Sum c()

500 fl1 = 1
s9(1) = c(1) + c(2) + c(3) + c(4) + c(5) + c(6) + c(7) + c(8) + c(9)
s9(2) = c(10) + c(11) + c(12) + c(13) + c(14) + c(15) + c(16) + c(17) + c(18)
s9(3) = c(19) + c(20) + c(21) + c(22) + c(23) + c(24) + c(25) + c(26) + c(27)
s9(4) = c(28) + c(29) + c(30) + c(31) + c(32) + c(33) + c(34) + c(35) + c(36)
s9(5) = c(37) + c(38) + c(39) + c(40) + c(41) + c(42) + c(43) + c(44) + c(45)
s9(6) = c(46) + c(47) + c(48) + c(49) + c(50) + c(51) + c(52) + c(53) + c(54)
s9(7) = c(55) + c(56) + c(57) + c(58) + c(59) + c(60) + c(61) + c(62) + c(63)
s9(8) = c(64) + c(65) + c(66) + c(67) + c(68) + c(69) + c(70) + c(71) + c(72)
s9(9) = c(73) + c(74) + c(75) + c(76) + c(77) + c(78) + c(79) + c(80) + c(81)

s9(10) = c(1) + c(10) + c(19) + c(28) + c(37) + c(46) + c(55) + c(64) + c(73)
s9(11) = c(2) + c(11) + c(20) + c(29) + c(38) + c(47) + c(56) + c(65) + c(74)
s9(12) = c(3) + c(12) + c(21) + c(30) + c(39) + c(48) + c(57) + c(66) + c(75)
s9(13) = c(4) + c(13) + c(22) + c(31) + c(40) + c(49) + c(58) + c(67) + c(76)
s9(14) = c(5) + c(14) + c(23) + c(32) + c(41) + c(50) + c(59) + c(68) + c(77)
s9(15) = c(6) + c(15) + c(24) + c(33) + c(42) + c(51) + c(60) + c(69) + c(78)
s9(16) = c(7) + c(16) + c(25) + c(34) + c(43) + c(52) + c(61) + c(70) + c(79)
s9(17) = c(8) + c(17) + c(26) + c(35) + c(44) + c(53) + c(62) + c(71) + c(80)
s9(18) = c(9) + c(18) + c(27) + c(36) + c(45) + c(54) + c(63) + c(72) + c(81)

s9(19) = c(1) + c(11) + c(21) + c(31) + c(41) + c(51) + c(61) + c(71) + c(81)
s9(20) = c(9) + c(17) + c(25) + c(33) + c(41) + c(49) + c(57) + c(65) + c(73)

For i1 = 1 To 20
If s9(i1) <> 20049 Then fl1 = 0: Return
Next i1
Return

'   Print results (selected numbers)

740 Cells(n9, 81).Select
For i1 = 1 To 81
Cells(n9, i1).Value = a(i1)
Next i1
Cells(n9, 82).Value = j1
Cells(n9, 83).Value = j2
Return

745
For i1 = 1 To 81
Sheets("Klad2").Cells(n10, i1).Value = c(i1)     'Squared Elements
Next i1
Return

'   Print results (squares)

750 n2 = n2 + 1
If n2 = 9 Then
n2 = 1: k1 = k1 + 10: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 10
End If

Cells(k1, k2 + 1).Select
Cells(k1, k2 + 1).Font.Color = -4165632
Cells(k1, k2 + 1).Value = n9

i3 = 0
For i1 = 1 To 9
For i2 = 1 To 9
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a(i3)
Next i2
Next i1

Return

End Sub
```