' Combines Sudoku Comparable Pan Magic Squares
' Calculates Semi Pan Magic Cubes

' Tested with Office 2007 under Windows 7

```Sub CnstrCbs5b()

Dim b(3, 25), a(25), a1(125), s(20), m(32)

m(1) = 1:    m(2) = 2:    m(3) = 8:    m(4) = 28:   m(5) = 29:   m(6) = 30:   m(7) = 51:   m(8) = 52:
m(9) = 59:   m(10) = 88:  m(11) = 89:  m(12) = 90:  m(13) = 97:  m(14) = 102: m(15) = 111: m(16) = 118:
m(17) = 123: m(18) = 130: m(19) = 139: m(20) = 144: m(21) = 151: m(22) = 152: m(23) = 153: m(24) = 182:
m(25) = 189: m(26) = 190: m(27) = 211: m(28) = 212: m(29) = 213: m(30) = 233: m(31) = 239: m(32) = 240:

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

n4 = 240                 'Sudoku51: 240
'Sudoku52: 960

n2 = 0: n20 = 0: n9 = 0: k1 = 1: k2 = 1
s1 = 315

t1 = Timer

For j1 = 1 To 32
j11 = m(j1)
For j2 = 1 To n4
If j2 = j11 Then GoTo 20

For j3 = 1 To n4
If j3 = j11 Or j3 = j2 Then GoTo 30

j10 = j11: j20 = 1: GoSub 100                 'Read Sudoku Comparable Square 1
j10 = j2:  j20 = 2: GoSub 100                 'Read Sudoku Comparable Square 2
j10 = j3:  j20 = 3: GoSub 100                 'Read Sudoku Comparable Square 3

For j4 = 1 To 25
a(j4) = b(1, j4) + 5 * b(2, j4) + 25 * b(3, j4) + 1
a1(100 + j4) = a(j4)
Next j4

GoSub 200: If fl1 = 0 Then GoTo 30           'Check Magic Sum
GoSub 300: If fl1 = 0 Then GoTo 30           'Ckeck identical numbers
GoSub 900: If fl1 = 0 Then GoTo 30           'Ckeck Pairs
GoSub 400: If fl1 = 0 Then GoTo 30           'Calculate and check Semi Pan Magic Cube

' ********************** Test **************************

''        Cells(j1, 1).Value = j1
''        Cells(j1, 2).Value = j2
''        Cells(j1, 3).Value = j3

''        n9 = n9 + 1
''        Cells(j1, 4).Select: Cells(j1, 4).Value = n9

'*******************************************************

''       n9 = n9 + 1: GoSub 740 'Print results (selected numbers top squares)
''       n9 = n9 + 1: GoSub 750 'Print results (top squares)
n9 = n9 + 1: GoSub 500 'Print results (selected numbers cubes)
''       n9 = n9 + 1: GoSub 600 'Print results (5 plane view cubes)

30  Next j3
20  Next j2
Next j1

t2 = Timer

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

End

100 k11 = Int((j10 - 1) / 4)
k12 = j10 - k11 * 4

k21 = 1 + k11 * 6
k22 = 1 + (k12 - 1) * 6

i3 = 0
For i1 = 1 To 5
For i2 = 1 To 5
i3 = i3 + 1
b(j20, i3) = Sheets("Sudoku51").Cells(k21 + i1, k22 + i2).Value
Next i2
Next i1

Return

'   Check Magic Sum

200 fl1 = 1
s(1) = a(1) + a(2) + a(3) + a(4) + a(5)
s(2) = a(6) + a(7) + a(8) + a(9) + a(10)
s(3) = a(11) + a(12) + a(13) + a(14) + a(15)
s(4) = a(16) + a(17) + a(18) + a(19) + a(20)
s(5) = a(21) + a(22) + a(23) + a(24) + a(25)

s(6) = a(1) + a(6) + a(11) + a(16) + a(21)
s(7) = a(2) + a(7) + a(12) + a(17) + a(22)
s(8) = a(3) + a(8) + a(13) + a(18) + a(23)
s(9) = a(4) + a(9) + a(14) + a(19) + a(24)
s(10) = a(5) + a(10) + a(15) + a(20) + a(25)

s(11) = a(1) + a(7) + a(13) + a(19) + a(25)
s(12) = a(5) + a(9) + a(13) + a(17) + a(21)

'   Pan Diagonals

s(13) = a(2) + a(8) + a(14) + a(20) + a(21)
s(14) = a(3) + a(9) + a(15) + a(16) + a(22)
s(15) = a(4) + a(10) + a(11) + a(17) + a(23)
s(16) = a(5) + a(6) + a(12) + a(18) + a(24)

s(17) = a(4) + a(8) + a(12) + a(16) + a(25)
s(18) = a(3) + a(7) + a(11) + a(20) + a(24)
s(19) = a(2) + a(6) + a(15) + a(19) + a(23)
s(20) = a(1) + a(10) + a(14) + a(18) + a(22)

For i1 = 1 To 20                                       '12 for Magic, 20 for Pan Magic
If s(i1) <> s1 Then fl1 = 0: Return
Next i1

Return

'   Check identical numbers

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

'   Check Pairs

900 fl1 = 1: n25 = 0
For i1 = 1 To 24
a2 = 0.4 * s1 - a(i1)          'Complement
For i2 = (1 + i1) To 24
If a2 = a(i2) Then fl1 = 0: Return
'           If a2 = a(i2) Then n25 = n25 + 1
Next i2
Next i1
Return

'   Calculate Semi Pan Magic Cubes

400 fl1 = 1
a1(121) = 315 - a1(122) - a1(123) - a1(124) - a1(125):
a1(116) = 315 - a1(117) - a1(118) - a1(119) - a1(120):
a1(115) = a1(117) + a1(118) - a1(125):
a1(114) = 315 - a1(118) - a1(119) - a1(120) - a1(124):
a1(113) = 315 - a1(117) - a1(118) - a1(119) - a1(123):
a1(112) = a1(119) + a1(120) - a1(122):
a1(111) = a1(118) + a1(119) - a1(121):
a1(110) = 315 - a1(117) - a1(118) - a1(122) - a1(123):
a1(109) = -315 + a1(118) + a1(119) + a1(120) + a1(123) + a1(124) + a1(125):
a1(108) = -315 + a1(117) + a1(118) + a1(119) + a1(122) + a1(123) + a1(124):
a1(107) = 315 - a1(119) - a1(120) - a1(124) - a1(125):
a1(106) = 315 - a1(118) - a1(119) - a1(123) - a1(124):
a1(105) = -a1(120) + a1(122) + a1(123):
a1(104) = 315 - a1(119) - a1(123) - a1(124) - a1(125):
a1(103) = 315 - a1(118) - a1(122) - a1(123) - a1(124):
a1(102) = -a1(117) + a1(124) + a1(125):
a1(101) = -315 + a1(117) + a1(118) + a1(119) + a1(120) + a1(123) + a1(124):
a1(100) = -441 + a1(117) + a1(118) + a1(119) + a1(120) + a1(122) + 2 * a1(123) + a1(124):
a1(99) = 189 - a1(117) - a1(123):
a1(98) = 504 - a1(118) - 2 * a1(122) - 2 * a1(123) - 2 * a1(124):
a1(97) = 189 - a1(119) - a1(123):
a1(96) = -126 - a1(120) + a1(122) + 2 * a1(123) + a1(124):
a1(95) = 189 + a1(117) - a1(119) - a1(123) - a1(124):
a1(94) = 504 - a1(118) - 2 * a1(119) - 2 * a1(120) - a1(124) - a1(125):
a1(93) = -126 + a1(122) + a1(123) + a1(124):
a1(92) = -441 + a1(118) + 2 * a1(119) + 2 * a1(120) + a1(123) + a1(124) + a1(125):
a1(91) = 189 - a1(117) + a1(119) - a1(122) - a1(123):
a1(90) = -126 - a1(117) + a1(119) + a1(120) + a1(124) + a1(125):
a1(89) = -441 + a1(118) + 3 * a1(119) + 2 * a1(120) - a1(122) + a1(123) + a1(124) + a1(125):
a1(88) = -126 + a1(118) + a1(122) + a1(124):
a1(87) = 504 + a1(117) - a1(118) - 2 * a1(119) - 2 * a1(120) - 2 * a1(124) - a1(125):
a1(86) = 504 - a1(118) - 2 * a1(119) - a1(120) - a1(123) - a1(124) - a1(125):
a1(85) = 189 - a1(119) - 2 * a1(120) + a1(122) + a1(123) - a1(125):
a1(84) = 504 + a1(117) - a1(118) - 2 * a1(119) - a1(120) - a1(123) - 2 * a1(124) - a1(125):
a1(83) = 504 - a1(117) - a1(118) - a1(119) - a1(122) - 2 * a1(123) - a1(124):
a1(82) = -126 - a1(117) + 2 * a1(119) + a1(120) - a1(122) + a1(124) + a1(125):
a1(81) = -756 + a1(117) + 2 * a1(118) + 2 * a1(119) + 2 * a1(120) + a1(122) + 2 * a1(123) + 2 * a1(124) + a1(125):
a1(80) = 504 - a1(117) - a1(118) - 2 * a1(122) - 2 * a1(123) - a1(124):
a1(79) = -441 + a1(118) + a1(119) + a1(120) + a1(122) + a1(123) + 2 * a1(124) + a1(125):
a1(78) = -441 + a1(117) + a1(118) + a1(119) + a1(122) + 3 * a1(123) + a1(124):
a1(77) = 189 - a1(119) - a1(120) + a1(122) - a1(125):
a1(76) = 504 - a1(118) - a1(119) - a1(122) - 2 * a1(123) - 2 * a1(124):
a1(75) = 693 - a1(118) - a1(119) - 2 * a1(122) - 3 * a1(123) - 2 * a1(124) - a1(125):
a1(74) = 63 - a1(119) - a1(120) + a1(122) + a1(123):
a1(73) = -567 + a1(117) + a1(118) + a1(119) + 2 * a1(122) + 3 * a1(123) + 2 * a1(124):
a1(72) = -252 + a1(118) + a1(119) + a1(120) + a1(123) + a1(124):
a1(71) = 378 - a1(117) - a1(118) - a1(122) - 2 * a1(123) - a1(124) + a1(125):
a1(70) = -567 + a1(118) + 2 * a1(119) + a1(120) + a1(122) + 2 * a1(123) + 2 * a1(124) + a1(125):
a1(69) = -252 - a1(117) + a1(118) + 2 * a1(119) + 2 * a1(120) - a1(122) + a1(124) + a1(125):
a1(68) = 378 - a1(118) - a1(122) - 2 * a1(123) - a1(124):
a1(67) = 693 - a1(118) - 3 * a1(119) - 2 * a1(120) - a1(123) - 2 * a1(124) - a1(125):
a1(66) = 63 + a1(117) - a1(119) - a1(120) + a1(122) + a1(123) - a1(125):
a1(65) = 378 - a1(118) - 2 * a1(119) - 2 * a1(120) + a1(122) - a1(124):
a1(64) = 693 + a1(117) - a1(118) - 3 * a1(119) - 2 * a1(120) - a1(123) - 2 * a1(124) - 2 * a1(125):
a1(63) = 63:
a1(62) = 126 - a1(64): a1(61) = 126 - a1(65):
a1(60) = 126 - a1(66): a1(59) = 126 - a1(67): a1(58) = 126 - a1(68): a1(57) = 126 - a1(69): a1(56) = 126 - a1(70):
a1(55) = 126 - a1(71): a1(54) = 126 - a1(72): a1(53) = 126 - a1(73): a1(52) = 126 - a1(74): a1(51) = 126 - a1(75):
a1(50) = 126 - a1(76): a1(49) = 126 - a1(77): a1(48) = 126 - a1(78): a1(47) = 126 - a1(79): a1(46) = 126 - a1(80):
a1(45) = 126 - a1(81): a1(44) = 126 - a1(82): a1(43) = 126 - a1(83): a1(42) = 126 - a1(84): a1(41) = 126 - a1(85):
a1(40) = 126 - a1(86): a1(39) = 126 - a1(87): a1(38) = 126 - a1(88): a1(37) = 126 - a1(89): a1(36) = 126 - a1(90):
a1(35) = 126 - a1(91): a1(34) = 126 - a1(92): a1(33) = 126 - a1(93): a1(32) = 126 - a1(94): a1(31) = 126 - a1(95):
a1(30) = 126 - a1(96): a1(29) = 126 - a1(97): a1(28) = 126 - a1(98): a1(27) = 126 - a1(99): a1(26) = 126 - a1(100):
a1(25) = 126 - a1(101): a1(24) = 126 - a1(102): a1(23) = 126 - a1(103): a1(22) = 126 - a1(104): a1(21) = 126 - a1(105):
a1(20) = 126 - a1(106): a1(19) = 126 - a1(107): a1(18) = 126 - a1(108): a1(17) = 126 - a1(109): a1(16) = 126 - a1(110):
a1(15) = 126 - a1(111): a1(14) = 126 - a1(112): a1(13) = 126 - a1(113): a1(12) = 126 - a1(114): a1(11) = 126 - a1(115):
a1(10) = 126 - a1(116): a1(9) = 126 - a1(117): a1(8) = 126 - a1(118): a1(7) = 126 - a1(119): a1(6) = 126 - a1(120):
a1(5) = 126 - a1(121): a1(4) = 126 - a1(122): a1(3) = 126 - a1(123): a1(2) = 126 - a1(124): a1(1) = 126 - a1(125):

For i1 = 1 To 125
a2 = a1(i1)
If a2 <= 0 Or a2 > 125 Then fl1 = 0: Return
For i2 = (1 + i1) To 125
If a2 = a1(i2) Then fl1 = 0: Return
Next i2
Next i1

Return

'   Print results (selected numbers)

500 Cells(n9, 125).Select
For i1 = 1 To 125
Cells(n9, i1).Value = a1(i1)
Next i1

Return

'   Print results (planes 11, 12, 13, 14 and 15)

600 n20 = n20 + 1
If n20 = 7 Then
n20 = 1: k1 = k1 + 30: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 6
End If

For i0 = 1 To 5
i3 = (5 - i0) * 25
For i1 = 1 To 5
For i2 = 1 To 5
i3 = i3 + 1
Cells(k1 + i1 + (i0 - 1) * 6, k2 + i2).Value = a1(i3)
Next i2
Next i1
If i0 = 1 Then
Cells(k1 + (i0 - 1) * 6, k2 + 1).Value = "Plane 1" + CStr(i0) + ", C" + CStr(n9)
Else
Cells(k1 + (i0 - 1) * 6, k2 + 1).Value = "Plane 1" + CStr(i0)
End If
Next i0

Return

'   Print results (selected numbers top squares)

740 Cells(n9, 25).Select
For i1 = 1 To 25
Cells(n9, i1).Value = a(i1)
Next i1
Return

'   Print results (top squares)

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

Return

End Sub
```