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

' Tested with Office 2007 under Windows 7

```Sub CnstrSqrs8a()

Dim b(3, 64), a(64), c(64), s9(18)

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

n2 = 0: n9 = 0: k1 = 1: k2 = 1
n20 = 0: n10 = 0: k10 = 1: k20 = 1
s1 = 260

t1 = Timer

For j1 = 1 To 1008

For j2 = 1 To 864

j10 = j1: j20 = 1: sht1 = "B1": GoSub 100              'Read Sudoku Comparable Square 1
j10 = j2: j20 = 2: sht1 = "B2": GoSub 100              'Read Sudoku Comparable Square 2

For j4 = 1 To 64
a(j4) = b(1, j4) + 8 * 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()

Cells(n9, 67).Value = "Bimagic"
n10 = n10 + 1: GoSub 745                     'Print results (squared numbers)
'       n10 = n10 + 1: GoSub 755                     'Print results (squares)

20  Next j2

Next j1

t2 = Timer

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

End

'   Read Sudoku Comparable Squares (Line Format)

100 For i1 = 1 To 64
b(j20, i1) = Sheets(sht1).Cells(j10, i1).Value
Next i1
Return

'   Check identical numbers

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

'   Construct Square c() (Squared Elements)

400 For i1 = 1 To 64
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)
s9(2) = c(9) + c(10) + c(11) + c(12) + c(13) + c(14) + c(15) + c(16)
s9(3) = c(17) + c(18) + c(19) + c(20) + c(21) + c(22) + c(23) + c(24)
s9(4) = c(25) + c(26) + c(27) + c(28) + c(29) + c(30) + c(31) + c(32)
s9(5) = c(33) + c(34) + c(35) + c(36) + c(37) + c(38) + c(39) + c(40)
s9(6) = c(41) + c(42) + c(43) + c(44) + c(45) + c(46) + c(47) + c(48)
s9(7) = c(49) + c(50) + c(51) + c(52) + c(53) + c(54) + c(55) + c(56)
s9(8) = c(57) + c(58) + c(59) + c(60) + c(61) + c(62) + c(63) + c(64)

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

s9(17) = c(1) + c(10) + c(19) + c(28) + c(37) + c(46) + c(55) + c(64)
s9(18) = c(8) + c(15) + c(22) + c(29) + c(36) + c(43) + c(50) + c(57)

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

'   Print results (selected numbers)

740 Cells(n9, 64).Select
For i1 = 1 To 64
Cells(n9, i1).Value = a(i1)
Next i1
Cells(n9, 65).Value = j1
Cells(n9, 66).Value = j2
Return

'   Print results (selected squared numbers)

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

'   Print results (squares)

750 n2 = n2 + 1
If n2 = 5 Then
n2 = 1: k1 = k1 + 9: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 9
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 8
For i2 = 1 To 8
i3 = i3 + 1
Cells(k1 + i1, k2 + i2).Value = a(i3)
Next i2
Next i1

Return

'   Print results (squared numbers, squares)

755 n20 = n20 + 1
If n20 = 5 Then
n20 = 1: k10 = k10 + 9: k20 = 1
Else
If n10 > 1 Then k20 = k20 + 9
End If

Sheets("Klad2").Cells(k10, k20 + 1).Font.Color = -4165632
Sheets("Klad2").Cells(k10, k20 + 1).Value = n9

i3 = 0
For i1 = 1 To 8
For i2 = 1 To 8
i3 = i3 + 1
Sheets("Klad2").Cells(k10 + i1, k20 + i2).Value = c(i3)
Next i2
Next i1

Return

End Sub
```