' Generates Pan Magic Squares of order 8, magic sum 260, Franklin Conditions, Pan Magic Sub Squares

' Tested with Office 2007 under Windows 7

```Sub MgcSqr8h()

Dim a(64), b(64), c(64)

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

n2 = 0: n9 = 0
m1 = 1: m2 = 64: s1 = 260

'  Generate data

t1 = Timer

For j64 = m1 To m2                                          'a(64)
If b(j64) = 0 Then b(j64) = j64: c(64) = j64 Else GoTo 640
a(64) = j64
For j63 = m1 To m2                                          'a(63)
If b(j63) = 0 Then b(j63) = j63: c(63) = j63 Else GoTo 630
a(63) = j63
For j62 = m1 To m2                                          'a(62)

If b(j62) = 0 Then b(j62) = j62: c(62) = j62 Else GoTo 620
j61 = 0.5 * s1 - j62 - a(63) - a(64)
If j61 <= 0 Or j61 > 64 Then GoTo 610                       'a(61)
If b(j61) = 0 Then b(j61) = j61: c(61) = j61 Else GoTo 610
a(62) = j62: a(61) = j61

For j60 = m1 To m2                                          'a(60)
If b(j60) = 0 Then b(j60) = j60: c(60) = j60 Else GoTo 600
a(60) = j60
For j59 = m1 To m2                                          'a(59)
If b(j59) = 0 Then b(j59) = j59: c(59) = j59 Else GoTo 590
j58 = -a(60) + a(62) + a(64)
If j58 <= 0 Or j58 > 64 Then GoTo 580                       'a(58)
If b(j58) = 0 Then b(j58) = j58: c(58) = j58 Else GoTo 580
j57 = 0.5 * s1 - j59 - a(62) - a(64)
If j57 <= 0 Or j57 > 64 Then GoTo 570                       'a(57)
If b(j57) = 0 Then b(j57) = j57: c(57) = j57 Else GoTo 570
a(59) = j59: a(58) = j58: a(57) = j57

For j56 = m1 To m2                                          'a(56)
If b(j56) = 0 Then b(j56) = j56: c(56) = j56 Else GoTo 560

j55 = 0.5 * s1 - j56 - a(63) - a(64)
If j55 <= 0 Or j55 > 64 Then GoTo 550                       'a(55)
If b(j55) = 0 Then b(j55) = j55: c(55) = j55 Else GoTo 550
j54 = j56 - a(62) + a(64)
If j54 <= 0 Or j54 > 64 Then GoTo 540                       'a(54)
If b(j54) = 0 Then b(j54) = j54: c(54) = j54 Else GoTo 540
j53 = -j56 + a(62) + a(63)
If j53 <= 0 Or j53 > 64 Then GoTo 530                       'a(53)
If b(j53) = 0 Then b(j53) = j53: c(53) = j53 Else GoTo 530
j52 = j56 - a(60) + a(64)
If j52 <= 0 Or j52 > 64 Then GoTo 520                       'a(52)
If b(j52) = 0 Then b(j52) = j52: c(52) = j52 Else GoTo 520
j51 = 0.5 * s1 - j56 - a(59) - a(64)
If j51 <= 0 Or j51 > 64 Then GoTo 510                       'a(51)
If b(j51) = 0 Then b(j51) = j51: c(51) = j51 Else GoTo 510
j50 = j56 + a(60) - a(62)
If j50 <= 0 Or j50 > 64 Then GoTo 500                       'a(50)
If b(j50) = 0 Then b(j50) = j50: c(50) = j50 Else GoTo 500
j49 = -j56 + a(59) + a(62)
If j49 <= 0 Or j49 > 64 Then GoTo 490                       'a(49)
If b(j49) = 0 Then b(j49) = j49: c(49) = j49 Else GoTo 490

a(56) = j56: a(55) = j55: a(54) = j54: a(53) = j53
a(52) = j52: a(51) = j51: a(50) = j50: a(49) = j49

j48 = 0.25 * s1 - a(62)
If j48 <= 0 Or j48 > 64 Then GoTo 480                       'a(48)
If b(j48) = 0 Then b(j48) = j48: c(48) = j48 Else GoTo 480
j47 = -0.25 * s1 + a(62) + a(63) + a(64)
If j47 <= 0 Or j47 > 64 Then GoTo 470                       'a(47)
If b(j47) = 0 Then b(j47) = j47: c(47) = j47 Else GoTo 470
j46 = 0.25 * s1 - a(64)
If j46 <= 0 Or j46 > 64 Then GoTo 460                       'a(46)
If b(j46) = 0 Then b(j46) = j46: c(46) = j46 Else GoTo 460
j45 = 0.25 * s1 - a(63)
If j45 <= 0 Or j45 > 64 Then GoTo 450                       'a(45)
If b(j45) = 0 Then b(j45) = j45: c(45) = j45 Else GoTo 450
j44 = 0.25 * s1 + a(60) - a(62) - a(64)
If j44 <= 0 Or j44 > 64 Then GoTo 440                       'a(44)
If b(j44) = 0 Then b(j44) = j44: c(44) = j44 Else GoTo 440
j43 = -0.25 * s1 + a(59) + a(62) + a(64)
If j43 <= 0 Or j43 > 64 Then GoTo 430                       'a(43)
If b(j43) = 0 Then b(j43) = j43: c(43) = j43 Else GoTo 430
j42 = 0.25 * s1 - a(60)
If j42 <= 0 Or j42 > 64 Then GoTo 420                       'a(42)
If b(j42) = 0 Then b(j42) = j42: c(42) = j42 Else GoTo 420
j41 = 0.25 * s1 - a(59)
If j41 <= 0 Or j41 > 64 Then GoTo 410                       'a(41)
If b(j41) = 0 Then b(j41) = j41: c(41) = j41 Else GoTo 410
j40 = 0.25 * s1 - a(56) + a(62) - a(64)
If j40 <= 0 Or j40 > 64 Then GoTo 400                       'a(40)
If b(j40) = 0 Then b(j40) = j40: c(40) = j40 Else GoTo 400
j39 = 0.25 * s1 + a(56) - a(62) - a(63)
If j39 <= 0 Or j39 > 64 Then GoTo 390                       'a(39)
If b(j39) = 0 Then b(j39) = j39: c(39) = j39 Else GoTo 390
j38 = 0.25 * s1 - a(56)
If j38 <= 0 Or j38 > 64 Then GoTo 380                       'a(38)
If b(j38) = 0 Then b(j38) = j38: c(38) = j38 Else GoTo 380
j37 = -0.25 * s1 + a(56) + a(63) + a(64)
If j37 <= 0 Or j37 > 64 Then GoTo 370                       'a(37)
If b(j37) = 0 Then b(j37) = j37: c(37) = j37 Else GoTo 370
j36 = 0.25 * s1 - a(56) - a(60) + a(62)
If j36 <= 0 Or j36 > 64 Then GoTo 360                       'a(36)
If b(j36) = 0 Then b(j36) = j36: c(36) = j36 Else GoTo 360
j35 = 0.25 * s1 + a(56) - a(59) - a(62)
If j35 <= 0 Or j35 > 64 Then GoTo 350                       'a(35)
If b(j35) = 0 Then b(j35) = j35: c(35) = j35 Else GoTo 350
j34 = 0.25 * s1 - a(56) + a(60) - a(64)
If j34 <= 0 Or j34 > 64 Then GoTo 340                       'a(34)
If b(j34) = 0 Then b(j34) = j34: c(34) = j34 Else GoTo 340
j33 = -0.25 * s1 + a(56) + a(59) + a(64)
If j33 <= 0 Or j33 > 64 Then GoTo 330                       'a(33)
If b(j33) = 0 Then b(j33) = j33: c(33) = j33 Else GoTo 330

a(48) = j48: a(47) = j47: a(46) = j46: a(45) = j45
a(44) = j44: a(43) = j43: a(42) = j42: a(41) = j41
a(40) = j40: a(39) = j39: a(38) = j38: a(37) = j37
a(36) = j36: a(35) = j35: a(34) = j34: a(33) = j33

For j32 = m1 To m2                                            'a(32)
If b(j32) = 0 Then b(j32) = j32: c(32) = j32 Else GoTo 320
j31 = -j32 + a(63) + a(64)
If j31 <= 0 Or j31 > 64 Then GoTo 310                       'a(31)
If b(j31) = 0 Then b(j31) = j31: c(31) = j31 Else GoTo 310
j30 = j32 + a(62) - a(64)
If j30 <= 0 Or j30 > 64 Then GoTo 300                       'a(30)
If b(j30) = 0 Then b(j30) = j30: c(30) = j30 Else GoTo 300
j29 = 0.5 * s1 - j32 - a(62) - a(63)
If j29 <= 0 Or j29 > 64 Then GoTo 290                       'a(29)
If b(j29) = 0 Then b(j29) = j29: c(29) = j29 Else GoTo 290
j28 = j32 + a(60) - a(64)
If j28 <= 0 Or j28 > 64 Then GoTo 280                       'a(28)
If b(j28) = 0 Then b(j28) = j28: c(28) = j28 Else GoTo 280
j27 = -j32 + a(59) + a(64)
If j27 <= 0 Or j27 > 64 Then GoTo 270                       'a(27)
If b(j27) = 0 Then b(j27) = j27: c(27) = j27 Else GoTo 270
j26 = j32 - a(60) + a(62)
If j26 <= 0 Or j26 > 64 Then GoTo 260                       'a(26)
If b(j26) = 0 Then b(j26) = j26: c(26) = j26 Else GoTo 260
j25 = 0.5 * s1 - j32 - a(59) - a(62)
If j25 <= 0 Or j25 > 64 Then GoTo 250                       'a(25)
If b(j25) = 0 Then b(j25) = j25: c(25) = j25 Else GoTo 250

a(32) = j32: a(31) = j31: a(30) = j30: a(29) = j29
a(28) = j28: a(27) = j27: a(26) = j26: a(25) = j25

For j24 = m1 To m2                                           'a(24)
If b(j24) = 0 Then b(j24) = j24: c(24) = j24 Else GoTo 240
a(24) = j24

a(23) = 0.5 * s1 - a(24) - a(63) - a(64)
If a(23) <= 0 Or a(23) > 64 Then GoTo 230
a(22) = a(24) - a(62) + a(64)
If a(22) <= 0 Or a(22) > 64 Then GoTo 230
a(21) = -a(24) + a(62) + a(63)
If a(21) <= 0 Or a(21) > 64 Then GoTo 230
a(20) = a(24) - a(60) + a(64)
If a(20) <= 0 Or a(20) > 64 Then GoTo 230
a(19) = 0.5 * s1 - a(24) - a(59) - a(64)
If a(19) <= 0 Or a(19) > 64 Then GoTo 230
a(18) = a(24) + a(60) - a(62)
If a(18) <= 0 Or a(18) > 64 Then GoTo 230
a(17) = -a(24) + a(59) + a(62)
If a(17) <= 0 Or a(17) > 64 Then GoTo 230
a(16) = 0.25 * s1 - a(32) - a(62) + a(64)
If a(16) <= 0 Or a(16) > 64 Then GoTo 230
a(15) = -0.25 * s1 + a(32) + a(62) + a(63)
If a(15) <= 0 Or a(15) > 64 Then GoTo 230
a(14) = 0.25 * s1 - a(32)
If a(14) <= 0 Or a(14) > 64 Then GoTo 230
a(13) = 0.25 * s1 + a(32) - a(63) - a(64)
If a(13) <= 0 Or a(13) > 64 Then GoTo 230
a(12) = 0.25 * s1 - a(32) + a(60) - a(62)
If a(12) <= 0 Or a(12) > 64 Then GoTo 230
a(11) = -0.25 * s1 + a(32) + a(59) + a(62)
If a(11) <= 0 Or a(11) > 64 Then GoTo 230
a(10) = 0.25 * s1 - a(32) - a(60) + a(64)
If a(10) <= 0 Or a(10) > 64 Then GoTo 230
a(9) = 0.25 * s1 + a(32) - a(59) - a(64)
If a(9) <= 0 Or a(9) > 64 Then GoTo 230
a(8) = 0.25 * s1 - a(24) + a(62) - a(64)
If a(8) <= 0 Or a(8) > 64 Then GoTo 230
a(7) = 0.25 * s1 + a(24) - a(62) - a(63)
If a(7) <= 0 Or a(7) > 64 Then GoTo 230
a(6) = 0.25 * s1 - a(24)
If a(6) <= 0 Or a(6) > 64 Then GoTo 230
a(5) = -0.25 * s1 + a(24) + a(63) + a(64)
If a(5) <= 0 Or a(5) > 64 Then GoTo 230
a(4) = 0.25 * s1 - a(24) - a(60) + a(62)
If a(4) <= 0 Or a(4) > 64 Then GoTo 230
a(3) = 0.25 * s1 + a(24) - a(59) - a(62)
If a(3) <= 0 Or a(3) > 64 Then GoTo 230
a(2) = 0.25 * s1 - a(24) + a(60) - a(64)
If a(2) <= 0 Or a(2) > 64 Then GoTo 230
a(1) = -0.25 * s1 + a(24) + a(59) + a(64)
If a(1) <= 0 Or a(1) > 64 Then GoTo 230

'                          Exclude solutions with identical numbers

GoSub 800: If fl1 = 0 Then GoTo 230

n9 = n9 + 1
GoSub 650 'Print results (squares)
'                          GoSub 645 'Print results (selected numbers)

230 b(c(24)) = 0: c(24) = 0
240 Next j24

b(c(25)) = 0: c(25) = 0
250 b(c(26)) = 0: c(26) = 0
260 b(c(27)) = 0: c(27) = 0
270 b(c(28)) = 0: c(28) = 0
280 b(c(29)) = 0: c(29) = 0
290 b(c(30)) = 0: c(30) = 0
300 b(c(31)) = 0: c(31) = 0
310 b(c(32)) = 0: c(32) = 0
320 Next j32

b(c(33)) = 0: c(33) = 0
330 b(c(34)) = 0: c(34) = 0
340 b(c(35)) = 0: c(35) = 0
350 b(c(36)) = 0: c(36) = 0
360 b(c(37)) = 0: c(37) = 0
370 b(c(38)) = 0: c(38) = 0
380 b(c(39)) = 0: c(39) = 0
390 b(c(40)) = 0: c(40) = 0
400 b(c(41)) = 0: c(41) = 0
410 b(c(42)) = 0: c(42) = 0
420 b(c(43)) = 0: c(43) = 0
430 b(c(44)) = 0: c(44) = 0
440 b(c(45)) = 0: c(45) = 0
450 b(c(46)) = 0: c(46) = 0
460 b(c(47)) = 0: c(47) = 0
470 b(c(48)) = 0: c(48) = 0
480 b(c(49)) = 0: c(49) = 0
490 b(c(50)) = 0: c(50) = 0
500 b(c(51)) = 0: c(51) = 0
510 b(c(52)) = 0: c(52) = 0
520 b(c(53)) = 0: c(53) = 0
530 b(c(54)) = 0: c(54) = 0
540 b(c(55)) = 0: c(55) = 0
550 b(c(56)) = 0: c(56) = 0
560 Next j56

b(c(57)) = 0: c(57) = 0
570 b(c(58)) = 0: c(58) = 0
580 b(c(59)) = 0: c(59) = 0
590 Next j59
b(c(60)) = 0: c(60) = 0
600 Next j60

b(c(61)) = 0: c(61) = 0
610 b(c(62)) = 0: c(62) = 0
620 Next j62
b(c(63)) = 0: c(63) = 0
630 Next j63
b(c(64)) = 0: c(64) = 0
640 Next j64

t2 = Timer

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

End

'   Print results (selected numbers)

645 For i1 = 1 To 64
Cells(n9, i1).Value = a(i1)
Next i1

Return

'   Print results (squares)

650 n2 = n2 + 1
If n2 = 5 Then
n2 = 1: k1 = k1 + 9: k2 = 0
Else
If n9 > 1 Then k2 = k2 + 9
End If

Cells(k1 + 1, k2 + 1).Select

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

'   Exclude solutions with identical numbers

800 fl1 = 1
For j1 = 1 To 64
a2 = a(j1)
For j2 = (1 + j1) To 64
If a2 = a(j2) Then fl1 = 0: GoTo 850
Next j2
Next j1
850 Return

End Sub
```