' Generates Bordered Magic Cubes of order 5 (Natural Numbers)

' Tested with Office 2007 under Windows 7

```Sub MgcCube5i()

Dim a1(125), a(50), b1(125), b(125), c(50), c5(125)
Dim a2(50), c2(50)  'Left Square

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

n2 = 0: n9 = 0: k1 = 1: k2 = 1
ShtNm1 = "AntSym5b": ShtNm2 = "Pairs73"

t1 = Timer

For j100 = 19 To 30

'   Start reading data from "AntSym5b"

Rcrd1a = Sheets(ShtNm1).Cells(j100, 28).Value
MC5 = Sheets(ShtNm1).Cells(j100, 26).Value

'   Read Natural Numbers From sheet "Pairs73"

s1 = Sheets(ShtNm2).Cells(Rcrd1a, 4).Value              'MC5
Pr5 = Sheets(ShtNm2).Cells(Rcrd1a, 1).Value
Cntr5 = Sheets(ShtNm2).Cells(Rcrd1a, 6).Value
nVar = Sheets(ShtNm2).Cells(Rcrd1a, 9).Value

m1 = 1: m2 = nVar

Erase b1
For j1 = 1 To nVar
x = Sheets(ShtNm2).Cells(Rcrd1a, 9 + j1).Value
b1(x) = x
Next j1
pMax = Sheets(ShtNm2).Cells(Rcrd1a, 9 + nVar).Value

m1 = 1: m2 = nVar

For i1 = 1 To 25
a(i1) = Sheets(ShtNm1).Cells(j100, i1).Value
Next i1
GoSub 750   'Fill Top and Bottom c5()

'   Remove used numbers and complements from available numbers

For i1 = 1 To 25
b1(c5(i1)) = 0: b1(c5(100 + i1)) = 0
Next i1

'   Store available pairs in a1()

n10 = 0
For j1 = 1 To pMax
If b1(j1) <> 0 Then
n10 = n10 + 1
a1(n10) = b1(j1)
End If
Next j1
m1 = 1: m2 = n10: n10 = 0

b(Cntr5) = Cntr5                'Block Center Element

'   Define Back Square

Erase a     'Clear Scratch Area
For i1 = 1 To 5
a(i1) = c5(i1): a(i1 + 20) = c5(i1 + 100)
Next i1

t1 = Timer

For j20 = m1 To m2                                          'a(20)
If b(a1(j20)) = 0 Then b(a1(j20)) = a1(j20): c(20) = a1(j20) Else GoTo 200
a(20) = a1(j20)

a(45) = Pr5 - a(20):    If b(a(45)) = 0 Then b(a(45)) = a(45): c(45) = a(45) Else GoTo 450

For j19 = m1 To m2                                            'a(19)
If b(a1(j19)) = 0 Then b(a1(j19)) = a1(j19): c(19) = a1(j19) Else GoTo 190
a(19) = a1(j19)

a(44) = Pr5 - a(19):    If b(a(44)) = 0 Then b(a(44)) = a(44): c(44) = a(44) Else GoTo 440

For j18 = m1 To m2                                            'a(18)
If b(a1(j18)) = 0 Then b(a1(j18)) = a1(j18): c(18) = a1(j18) Else GoTo 180
a(18) = a1(j18)

a(43) = Pr5 - a(18):    If b(a(43)) = 0 Then b(a(43)) = a(43): c(43) = a(43) Else GoTo 430

For j17 = m1 To m2                                             'a(17)
If b(a1(j17)) = 0 Then b(a1(j17)) = a1(j17): c(17) = a1(j17) Else GoTo 170
a(17) = a1(j17)

a(42) = Pr5 - a(17):    If b(a(42)) = 0 Then b(a(42)) = a(42): c(42) = a(42) Else GoTo 420

a(16) = s1 - a(17) - a(18) - a(19) - a(20):
If a(16) < a1(m1) Or a(16) > a1(m2) Then GoTo 160:
If b1(a(16)) = 0 Then GoTo 160
If b(a(16)) = 0 Then b(a(16)) = a(16): c(16) = a(16) Else GoTo 160

a(41) = Pr5 - a(16):    If b(a(41)) = 0 Then b(a(41)) = a(41): c(41) = a(41) Else GoTo 410

For j14 = m1 To m2                                          'a(14)
If b(a1(j14)) = 0 Then b(a1(j14)) = a1(j14): c(14) = a1(j14) Else GoTo 140
a(14) = a1(j14)

a(39) = Pr5 - a(14):    If b(a(39)) = 0 Then b(a(39)) = a(39): c(39) = a(39) Else GoTo 390

a(13) = a(14) - a(17) + a(19) + a(4) - a(5) - a(21) + a(24)
If a(13) < a1(m1) Or a(13) > a1(m2) Then GoTo 130
If b1(a(13)) = 0 Then GoTo 130
If b(a(13)) = 0 Then b(a(13)) = a(13): c(13) = a(13) Else GoTo 130

a(38) = Pr5 - a(13):    If b(a(38)) = 0 Then b(a(38)) = a(38): c(38) = a(38) Else GoTo 380

a(9) = s1 - a(14) - a(19) - a(4) - a(24)
If a(9) < a1(m1) Or a(9) > a1(m2) Then GoTo 90
If b1(a(9)) = 0 Then GoTo 90
If b(a(9)) = 0 Then b(a(9)) = a(9): c(9) = a(9) Else GoTo 90

a(34) = Pr5 - a(9): If b(a(34)) = 0 Then b(a(34)) = a(34): c(34) = a(34) Else GoTo 340

a(8) = s1 - a(13) - a(18) - a(3) - a(23)
If a(8) < a1(m1) Or a(8) > a1(m2) Then GoTo 80
If b1(a(8)) = 0 Then GoTo 80
If b(a(8)) = 0 Then b(a(8)) = a(8): c(8) = a(8) Else GoTo 80

a(33) = Pr5 - a(8): If b(a(33)) = 0 Then b(a(33)) = a(33): c(33) = a(33) Else GoTo 330

a(7) = s1 - a(13) - a(19) - a(1) - a(25)
If a(7) < a1(m1) Or a(7) > a1(m2) Then GoTo 70
If b1(a(7)) = 0 Then GoTo 70
If b(a(7)) = 0 Then b(a(7)) = a(7): c(7) = a(7) Else GoTo 70

a(32) = Pr5 - a(7): If b(a(32)) = 0 Then b(a(32)) = a(32): c(32) = a(32) Else GoTo 320

a(12) = s1 - a(7) - a(17) - a(2) - a(22)
If a(12) < a1(m1) Or a(12) > a1(m2) Then GoTo 120
If b1(a(12)) = 0 Then GoTo 120
If b(a(12)) = 0 Then b(a(12)) = a(12): c(12) = a(12) Else GoTo 120

a(37) = Pr5 - a(12):    If b(a(37)) = 0 Then b(a(37)) = a(37): c(37) = a(37) Else GoTo 370

For j15 = m1 To m2                                          'a(15)
If b(a1(j15)) = 0 Then b(a1(j15)) = a1(j15): c(15) = a1(j15) Else GoTo 150
a(15) = a1(j15)

a(40) = Pr5 - a(15):    If b(a(40)) = 0 Then b(a(40)) = a(40): c(40) = a(40) Else GoTo 400

a(10) = s1 - a(15) - a(20) - a(5) - a(25)
If a(10) < a1(m1) Or a(10) > a1(m2) Then GoTo 100
If b1(a(10)) = 0 Then GoTo 100
If b(a(10)) = 0 Then b(a(10)) = a(10): c(10) = a(10) Else GoTo 100

a(35) = Pr5 - a(10):    If b(a(35)) = 0 Then b(a(35)) = a(35): c(35) = a(35) Else GoTo 350

a(6) = s1 - a(7) - a(8) - a(9) - a(10)
If a(6) < a1(m1) Or a(6) > a1(m2) Then GoTo 60
If b1(a(6)) = 0 Then GoTo 60
If b(a(6)) = 0 Then b(a(6)) = a(6): c(6) = a(6) Else GoTo 60

a(31) = Pr5 - a(6): If b(a(31)) = 0 Then b(a(31)) = a(31): c(31) = a(31) Else GoTo 310

a(11) = s1 - a(6) - a(16) - a(1) - a(21)
If a(11) < a1(m1) Or a(11) > a1(m2) Then GoTo 110
If b1(a(11)) = 0 Then GoTo 110
If b(a(11)) = 0 Then b(a(11)) = a(11): c(11) = a(11) Else GoTo 110

a(36) = Pr5 - a(11):    If b(a(36)) = 0 Then b(a(36)) = a(36): c(36) = a(36) Else GoTo 360

GoSub 760                                 'Fill Back and Front c5()
GoSub 3000                                'Determine Left Square (Magic)

If fl2 = 1 Then Erase b, c, c5: GoTo 1000 'Print only first cube

5

b(c(36)) = 0: c(36) = 0
360 b(c(11)) = 0: c(11) = 0
110 b(c(31)) = 0: c(31) = 0
310 b(c(6)) = 0: c(6) = 0
60  b(c(35)) = 0: c(35) = 0
350 b(c(10)) = 0: c(10) = 0
100 b(c(40)) = 0: c(40) = 0
400 b(c(15)) = 0: c(15) = 0
150 Next j15

b(c(37)) = 0: c(37) = 0
370 b(c(12)) = 0: c(12) = 0
120 b(c(32)) = 0: c(32) = 0
320 b(c(7)) = 0: c(7) = 0
70  b(c(33)) = 0: c(33) = 0
330 b(c(8)) = 0: c(8) = 0
80  b(c(34)) = 0: c(34) = 0
340 b(c(9)) = 0: c(9) = 0
90  b(c(38)) = 0: c(38) = 0
380 b(c(13)) = 0: c(13) = 0
130 b(c(39)) = 0: c(39) = 0
390 b(c(14)) = 0: c(14) = 0
140 Next j14

b(c(41)) = 0: c(41) = 0
410 b(c(16)) = 0: c(16) = 0
160 b(c(42)) = 0: c(42) = 0
420 b(c(17)) = 0: c(17) = 0
170 Next j17

b(c(43)) = 0: c(43) = 0
430 b(c(18)) = 0: c(18) = 0
180 Next j18

b(c(44)) = 0: c(44) = 0
440 b(c(19)) = 0: c(19) = 0
190 Next j19

b(c(45)) = 0: c(45) = 0
450 b(c(20)) = 0: c(20) = 0
200 Next j20

1000  Erase b, c, c5
Next j100

t2 = Timer

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

End

'    Determine Left Magic Square and Complete Cube

3000 fl2 = 1

Erase a2, c2         'Clear Scratch Areas

a2(1) = c5(1):    a2(2) = c5(6):    a2(3) = c5(11):   a2(4) = c5(16):   a2(5) = c5(21):
a2(6) = c5(26):                                                         a2(10) = c5(46):
a2(11) = c5(51):                                                        a2(15) = c5(71):
a2(16) = c5(76):                                                        a2(20) = c5(96):
a2(21) = c5(101): a2(22) = c5(106): a2(23) = c5(111): a2(24) = c5(116): a2(25) = c5(121):

For jj19 = m1 To m2                                            'a2(19)
If b(a1(jj19)) = 0 Then b(a1(jj19)) = a1(jj19): c2(19) = a1(jj19) Else GoTo 3190
a2(19) = a1(jj19)

a2(44) = Pr5 - a2(19):    If b(a2(44)) = 0 Then b(a2(44)) = a2(44): c2(44) = a2(44) Else GoTo 3440

For jj18 = m1 To m2                                            'a2(18)
If b(a1(jj18)) = 0 Then b(a1(jj18)) = a1(jj18): c2(18) = a1(jj18) Else GoTo 3180
a2(18) = a1(jj18)

a2(43) = Pr5 - a2(18):    If b(a2(43)) = 0 Then b(a2(43)) = a2(43): c2(43) = a2(43) Else GoTo 3430

a2(17) = s1 - a2(18) - a2(19) - a2(16) - a2(20)
If a2(17) < a1(m1) Or a2(17) > a1(m2) Then GoTo 3170
If b1(a2(17)) = 0 Then GoTo 3170
If b(a2(17)) = 0 Then b(a2(17)) = a2(17): c2(17) = a2(17) Else GoTo 3170

a2(42) = Pr5 - a2(17):    If b(a2(42)) = 0 Then b(a2(42)) = a2(42): c2(42) = a2(42) Else GoTo 3420

a2(14) = (-3*a2(18)-6*a2(19)-a2(11)-a2(15)-3*a2(16)-3*a2(20)+a2(1)+3*a2(2)+2*a2(3)+4*a2(5)+4*a2(21)+3*a2(22)+2*a2(23)+a2(25))/3
jj14 = Int(a2(14)): If jj14 <> a2(14) Then GoTo 3140
If a2(14) < a1(m1) Or a2(14) > a1(m2) Then GoTo 3140
If b1(a2(14)) = 0 Then GoTo 3140
If b(a2(14)) = 0 Then b(a2(14)) = a2(14): c2(14) = a2(14) Else GoTo 3140

a2(39) = Pr5 - a2(14):    If b(a2(39)) = 0 Then b(a2(39)) = a2(39): c2(39) = a2(39) Else GoTo 3390

a2(13) = a2(14) - a2(17) + a2(19) + a2(4) - a2(5) - a2(21) + a2(24)
If a2(13) < a1(m1) Or a2(13) > a1(m2) Then GoTo 3130
If b1(a2(13)) = 0 Then GoTo 3130
If b(a2(13)) = 0 Then b(a2(13)) = a2(13): c2(13) = a2(13) Else GoTo 3130

a2(38) = Pr5 - a2(13):    If b(a2(38)) = 0 Then b(a2(38)) = a2(38): c2(38) = a2(38) Else GoTo 3380

a2(12) = s1 - a2(13) - a2(14) - a2(11) - a2(15)
If a2(12) < a1(m1) Or a2(12) > a1(m2) Then GoTo 3120
If b1(a2(12)) = 0 Then GoTo 3120
If b(a2(12)) = 0 Then b(a2(12)) = a2(12): c2(12) = a2(12) Else GoTo 3120

a2(37) = Pr5 - a2(12):    If b(a2(37)) = 0 Then b(a2(37)) = a2(37): c2(37) = a2(37) Else GoTo 3370

a2(9) = s1 - a2(14) - a2(19) - a2(4) - a2(24)
If a2(9) < a1(m1) Or a2(9) > a1(m2) Then GoTo 3090
If b1(a2(9)) = 0 Then GoTo 3090
If b(a2(9)) = 0 Then b(a2(9)) = a2(9): c2(9) = a2(9) Else GoTo 3090

a2(34) = Pr5 - a2(9): If b(a2(34)) = 0 Then b(a2(34)) = a2(34): c2(34) = a2(34) Else GoTo 3340

a2(8) = s1 - a2(13) - a2(18) - a2(3) - a2(23)
If a2(8) < a1(m1) Or a2(8) > a1(m2) Then GoTo 3080
If b1(a2(8)) = 0 Then GoTo 3080
If b(a2(8)) = 0 Then b(a2(8)) = a2(8): c2(8) = a2(8) Else GoTo 3080

a2(33) = Pr5 - a2(8): If b(a2(33)) = 0 Then b(a2(33)) = a2(33): c2(33) = a2(33) Else GoTo 3330

a2(7) = s1 - a2(13) - a2(19) - a2(1) - a2(25)
If a2(7) < a1(m1) Or a2(7) > a1(m2) Then GoTo 3070
If b1(a2(7)) = 0 Then GoTo 3070
If b(a2(7)) = 0 Then b(a2(7)) = a2(7): c2(7) = a2(7) Else GoTo 3070

a2(32) = Pr5 - a2(7): If b(a2(32)) = 0 Then b(a2(32)) = a2(32): c2(32) = a2(32) Else GoTo 3320

GoSub 770                             'Fill Left and Right c5()

GoSub 800:                            'Back Check Identcal Numbers
If fl1 = 1 Then
n9 = n9 + 1: GoSub 1750           'Print results (Cubes)
Return
End If

b(c2(32)) = 0: c2(32) = 0
3320 b(c2(7)) = 0: c2(7) = 0
3070 b(c2(33)) = 0: c2(33) = 0
3330 b(c2(8)) = 0: c2(8) = 0
3080 b(c2(34)) = 0: c2(34) = 0
3340 b(c2(9)) = 0: c2(9) = 0
3090 b(c2(37)) = 0: c2(37) = 0
3370 b(c2(12)) = 0: c2(12) = 0
3120 b(c2(38)) = 0: c2(38) = 0
3380 b(c2(13)) = 0: c2(13) = 0
3130 b(c2(39)) = 0: c2(39) = 0
3390 b(c2(14)) = 0: c2(14) = 0
3140 b(c2(42)) = 0: c2(42) = 0
3420 b(c2(17)) = 0: c2(17) = 0
3170 b(c2(43)) = 0: c2(43) = 0
3430 b(c2(18)) = 0: c2(18) = 0
3180 Next jj18

b(c2(44)) = 0: c2(44) = 0
3440 b(c2(19)) = 0: c2(19) = 0
3190 Next jj19

fl2 = 0

Return

'   Fill Cube c5()

750 For i1 = 1 To 25        'Top Square
c5(i1) = a(i1)
Next i1

'   Bottom Square

c5(101) = Pr5 - a(25):  c5(102) = Pr5 - a(22):  c5(103) = Pr5 - a(23):  c5(104) = Pr5 - a(24):  c5(105) = Pr5 - a(21):
c5(106) = Pr5 - a(10):  c5(107) = Pr5 - a(7):   c5(108) = Pr5 - a(8):   c5(109) = Pr5 - a(9):   c5(110) = Pr5 - a(6):
c5(111) = Pr5 - a(15):  c5(112) = Pr5 - a(12):  c5(113) = Pr5 - a(13):  c5(114) = Pr5 - a(14):  c5(115) = Pr5 - a(11):
c5(116) = Pr5 - a(20):  c5(117) = Pr5 - a(17):  c5(118) = Pr5 - a(18):  c5(119) = Pr5 - a(19):  c5(120) = Pr5 - a(16):
c5(121) = Pr5 - a(5):   c5(122) = Pr5 - a(2):   c5(123) = Pr5 - a(3):   c5(124) = Pr5 - a(4):   c5(125) = Pr5 - a(1):

Return

760
'   Back  Square

c5(26) = a(6):  c5(27) = a(7):  c5(28) = a(8):  c5(29) = a(9):  c5(30) = a(10):
c5(51) = a(11): c5(52) = a(12): c5(53) = a(13): c5(54) = a(14): c5(55) = a(15):
c5(76) = a(16): c5(77) = a(17): c5(78) = a(18): c5(79) = a(19): c5(80) = a(20):

'   Front Square

c5(46) = Pr5 - a(10): c5(47) = Pr5 - a(7):  c5(48) = Pr5 - a(8):  c5(49) = Pr5 - a(9):  c5(50) = Pr5 - a(6):
c5(71) = Pr5 - a(15): c5(72) = Pr5 - a(12): c5(73) = Pr5 - a(13): c5(74) = Pr5 - a(14): c5(75) = Pr5 - a(11):
c5(96) = Pr5 - a(20): c5(97) = Pr5 - a(17): c5(98) = Pr5 - a(18): c5(99) = Pr5 - a(19): c5(100) = Pr5 - a(16):

Return

770
'   Left Square

c5(31) = a2(7):  c5(36) = a2(8):  c5(41) = a2(9):
c5(56) = a2(12): c5(61) = a2(13): c5(66) = a2(14):
c5(81) = a2(17): c5(86) = a2(18): c5(91) = a2(19):

'   Right Square

c5(35) = Pr5 - a2(7):  c5(40) = Pr5 - a2(8):  c5(45) = Pr5 - a2(9):
c5(60) = Pr5 - a2(12): c5(65) = Pr5 - a2(13): c5(70) = Pr5 - a2(14):
c5(85) = Pr5 - a2(17): c5(90) = Pr5 - a2(18): c5(95) = Pr5 - a2(19):

Return

'   Exclude solutions with identical numbers (Back Check)

800 fl1 = 1
For j1 = 1 To 125
c20 = c5(j1): If c20 = 0 Then GoTo 810
For j2 = (1 + j1) To 125
If c20 = c5(j2) Then fl1 = 0: Return
Next j2
810 Next j1
Return

'   Check Pairs

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

Return

'    Print results (5 plane format)

1750 n2 = n2 + 1
If n2 = 7 Then
n2 = 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 = c5(i3)
Next i2
Next i1
If i0 = 1 Then
Cells(k1 + (i0 - 1) * 6, k2 + 1).Font.Color = -4165632
Cells(k1 + (i0 - 1) * 6, k2 + 1).Value = "MC5 = " + CStr(s1)
End If
Next i0

Return

End Sub
```