About the Author

' Generates Almost Perfect Magic Cubes of order 4 for integers 1 thru 64

' Tested with Office 2007 under Windows 7

```Sub MgcCube4a()

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

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

n2 = 0: n9 = 0: k1 = 1: k2 = 1
m1 = 1: m2 = 64: s1 = 130

'   Generate data

Sheets("Klad1").Select

t1 = Timer

For j64 = 54 To 54                                                     'a(64)
If b(j64) = 0 Then b(j64) = j64: c(64) = j64 Else GoTo 640
a(64) = j64

For j63 = 19 To 19                                                     'a(63)
If b(j63) = 0 Then b(j63) = j63: c(63) = j63 Else GoTo 630
a(63) = j63

For j62 = 41 To 41                                                     'a(62)
If b(j62) = 0 Then b(j62) = j62: c(62) = j62 Else GoTo 620
a(62) = j62

a(61) = 130 - a(62) - a(63) - a(64)
If a(61) <= 0 Or a(61) > 64 Then GoTo 610                          'a(61)
If b(a(61)) = 0 Then b(a(61)) = a(61): c(61) = a(61) Else GoTo 610

For j60 = 28 To 28                                                     'a(60)
If b(j60) = 0 Then b(j60) = j60: c(60) = j60 Else GoTo 600
a(60) = j60

For j59 = 13 To 13                                                     'a(59)
If b(j59) = 0 Then b(j59) = j59: c(59) = j59 Else GoTo 590
a(59) = j59

For j58 = 55 To 55                                                     'a(58)
If b(j58) = 0 Then b(j58) = j58: c(58) = j58 Else GoTo 580
a(58) = j58

a(57) = 130 - a(58) - a(59) - a(60)
If a(57) <= 0 Or a(57) > 64 Then GoTo 570                          'a(57)
If b(a(57)) = 0 Then b(a(57)) = a(57): c(57) = a(57) Else GoTo 570

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

a(55) = -130 + a(56) - a(58) + a(60) + a(62) + a(63) + 2 * a(64)
If a(55) <= 0 Or a(55) > 64 Then GoTo 550                          'a(55)
If b(a(55)) = 0 Then b(a(55)) = a(55): c(55) = a(55) Else GoTo 550
a(54) = 130 - a(55) - a(58) - a(59)
If a(54) <= 0 Or a(54) > 64 Then GoTo 540                          'a(54)
If b(a(54)) = 0 Then b(a(54)) = a(54): c(54) = a(54) Else GoTo 540
a(53) = -a(56) + a(58) + a(59)
If a(53) <= 0 Or a(53) > 64 Then GoTo 530                          'a(53)
If b(a(53)) = 0 Then b(a(53)) = a(53): c(53) = a(53) Else GoTo 530
a(52) = 130 - a(56) - a(60) - a(64)
If a(52) <= 0 Or a(52) > 64 Then GoTo 520                          'a(52)
If b(a(52)) = 0 Then b(a(52)) = a(52): c(52) = a(52) Else GoTo 520
a(51) = 130 - a(55) - a(59) - a(63)
If a(51) <= 0 Or a(51) > 64 Then GoTo 510                          'a(51)
If b(a(51)) = 0 Then b(a(51)) = a(51): c(51) = a(51) Else GoTo 510
a(50) = a(55) + a(59) - a(62)
If a(50) <= 0 Or a(50) > 64 Then GoTo 500                          'a(50)
If b(a(50)) = 0 Then b(a(50)) = a(50): c(50) = a(50) Else GoTo 500
a(49) = a(55) + a(58) - a(64)
If a(49) <= 0 Or a(49) > 64 Then GoTo 490                          'a(49)
If b(a(49)) = 0 Then b(a(49)) = a(49): c(49) = a(49) Else GoTo 490

For j48 = m1 To m2                                                     'a(48)
If b(j48) = 0 Then b(j48) = j48: c(48) = j48 Else GoTo 480
a(48) = j48
For j47 = m1 To m2                                                     'a(47)
If b(j47) = 0 Then b(j47) = j47: c(47) = j47 Else GoTo 470
a(47) = j47
For j46 = m1 To m2                                                     'a(46)
If b(j46) = 0 Then b(j46) = j46: c(46) = j46 Else GoTo 460
a(46) = j46

a(45) = 130 - a(46) - a(47) - a(48)
If a(45) <= 0 Or a(45) > 64 Then GoTo 450                          'a(45)
If b(a(45)) = 0 Then b(a(45)) = a(45): c(45) = a(45) Else GoTo 450

For j44 = m1 To m2                                                     'a(44)
If b(j44) = 0 Then b(j44) = j44: c(44) = j44 Else GoTo 440
a(44) = j44

a(43) = 130 - a(44) - a(46) - a(48) - a(58) - a(60) + a(63) + a(64): If a(43) <= 0 Or a(43) > 64 Then GoTo 430
a(42) = 130 - a(43) - a(62) - a(63): If a(42) <= 0 Or a(42) > 64 Then GoTo 430
a(41) = -a(44) + a(62) + a(63): If a(41) <= 0 Or a(41) > 64 Then GoTo 430
a(40) = 130 + a(42) - a(47) - a(48) - a(56) + a(58) - a(63) - a(64): If a(40) <= 0 Or a(40) > 64 Then GoTo 430
a(39) = 130 - a(43) - a(56) - a(60): If a(39) <= 0 Or a(39) > 64 Then GoTo 430
a(38) = -a(42) + a(56) + a(60): If a(38) <= 0 Or a(38) > 64 Then GoTo 430
a(37) = 130 - a(40) - a(62) - a(63): If a(37) <= 0 Or a(37) > 64 Then GoTo 430
a(36) = 130 - a(40) - a(44) - a(48): If a(36) <= 0 Or a(36) > 64 Then GoTo 430
a(35) = -a(47) + a(56) + a(60): If a(35) <= 0 Or a(35) > 64 Then GoTo 430
a(34) = 130 - a(46) - a(56) - a(60): If a(34) <= 0 Or a(34) > 64 Then GoTo 430
a(33) = -a(36) + a(46) + a(47): If a(33) <= 0 Or a(33) > 64 Then GoTo 430
a(32) = 195 + a(44) + 1.5 * a(46) - 0.5 * a(47) - a(56) + 1.5 * a(58) - 0.5 * a(59) - a(62) - 3 * a(63) - 3 * a(64)
If a(32) <= 0 Or a(32) > 64 Then GoTo 430
If Int(a(32)) <> a(32) Then GoTo 430
a(31) = -130 + a(32) - a(46) + a(48) + a(62) + a(63) + 2 * a(64): If a(31) <= 0 Or a(31) > 64 Then GoTo 430
a(30) = 130 - a(31) - a(46) - a(47): If a(30) <= 0 Or a(30) > 64 Then GoTo 430
a(29) = -a(32) + a(46) + a(47): If a(29) <= 0 Or a(29) > 64 Then GoTo 430
a(28) = -a(30) - a(44) - a(46) + 2 * a(56) - 2 * a(58) + 2 * a(63) + 2 * a(64)
If a(28) <= 0 Or a(28) > 64 Then GoTo 430
a(27) = -a(32) + a(44) + a(46) + a(58) + a(60) - a(63) - a(64): If a(27) <= 0 Or a(27) > 64 Then GoTo 430
a(26) = -a(27) + a(62) + a(63): If a(26) <= 0 Or a(26) > 64 Then GoTo 430
a(25) = -a(27) - a(46) - a(48) + a(59) + a(60) + a(63) + a(64): If a(25) <= 0 Or a(25) > 64 Then GoTo 430
a(24) = 260 - a(32) - a(44) - a(48) - a(56) - a(60) - 2 * a(64): If a(24) <= 0 Or a(24) > 64 Then GoTo 430
a(23) = -a(27) + a(56) + a(60): If a(23) <= 0 Or a(23) > 64 Then GoTo 430
a(22) = 130 - a(23) - a(62) - a(63): If a(22) <= 0 Or a(22) > 64 Then GoTo 430
a(21) = -a(24) + a(62) + a(63): If a(21) <= 0 Or a(21) > 64 Then GoTo 430
a(20) = 130 - a(25) + a(44) - a(46) - a(47) - a(48) - a(56) + a(58) + a(59) + a(60) - a(62) - a(63)
If a(20) <= 0 Or a(20) > 64 Then GoTo 430
a(19) = -a(21) + a(44) + a(46): If a(19) <= 0 Or a(19) > 64 Then GoTo 430
a(18) = a(21) - a(44) + a(47): If a(18) <= 0 Or a(18) > 64 Then GoTo 430
a(17) = 130 - a(20) - a(46) - a(47): If a(17) <= 0 Or a(17) > 64 Then GoTo 430
a(16) = -130 - a(18) + a(47) + a(56) + a(60) + a(62) + a(63) + a(64): If a(16) <= 0 Or a(16) > 64 Then GoTo 430
a(15) = a(19) - a(47) + a(56) + a(60) - a(63): If a(15) <= 0 Or a(15) > 64 Then GoTo 430
a(14) = 130 - a(15) - a(62) - a(63): If a(14) <= 0 Or a(14) > 64 Then GoTo 430
a(13) = -a(16) + a(62) + a(63): If a(13) <= 0 Or a(13) > 64 Then GoTo 430
a(12) = 260 + a(20) - 2 * a(44) - a(48) - a(56) - 2 * a(60) - 2 * a(64): If a(12) <= 0 Or a(12) > 64 Then GoTo 430
a(11) = 130 + a(13) - a(59) - a(62) - a(63) - a(64): If a(11) <= 0 Or a(11) > 64 Then GoTo 430
a(10) = 130 - a(11) - a(58) - a(59): If a(10) <= 0 Or a(10) > 64 Then GoTo 430
a(9) = -a(17) + a(48) + a(56): If a(9) <= 0 Or a(9) > 64 Then GoTo 430
a(8) = 130 - a(12) - a(56) - a(60): If a(8) <= 0 Or a(8) > 64 Then GoTo 430
a(7) = -a(8) - a(46) + a(47) - a(58) - a(60) + 2 * a(63) + 2 * a(64): If a(7) <= 0 Or a(7) > 64 Then GoTo 430
a(6) = -260 + a(11) + a(56) + 2 * a(59) + a(60) + a(62) + a(63) + 2 * a(64): If a(6) <= 0 Or a(6) > 64 Then GoTo 430
a(5) = 390 - a(12) - 2 * a(44) - a(46) - a(47) - 2 * a(48) - a(56) - a(60) - 2 * a(64)
If a(5) <= 0 Or a(5) > 64 Then GoTo 430
a(4) = 130 + a(6) - a(59) - a(62) - a(63) - a(64): If a(4) <= 0 Or a(4) > 64 Then GoTo 430
a(3) = -a(5) + a(60) + a(62): If a(3) <= 0 Or a(3) > 64 Then GoTo 430
a(2) = -a(3) + a(62) + a(63): If a(2) <= 0 Or a(2) > 64 Then GoTo 430
a(1) = 130 - a(4) - a(62) - a(63): If a(1) <= 0 Or a(1) > 64 Then GoTo 430

'       Exclude solutions with identical numbers

GoSub 800: If fl1 = 0 Then GoTo 430

n9 = n9 + 1: GoSub 740 'Print results (selected numbers)
'       n9 = n9 + 1: GoSub 750 'Print results (planes 1, 2, 3)
'       n9 = n9 + 1: GoSub 760 'Print results (3d)

430 b(c(44)) = 0: c(44) = 0
440 Next j44

b(c(45)) = 0: c(45) = 0
450 b(c(46)) = 0: c(46) = 0
460 Next j46
b(c(47)) = 0: c(47) = 0
470 Next j47
b(c(48)) = 0: c(48) = 0
480 Next j48

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 Next j58
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 MgcCube4a")

End

'   Print results (selected numbers)

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

Return

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

750 n2 = n2 + 1
If n2 = 7 Then
n2 = 1: k1 = k1 + 20: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 5
End If

For i0 = 1 To 4
i3 = (4 - i0) * 16
For i1 = 1 To 4
For i2 = 1 To 4
i3 = i3 + 1
Cells(k1 + i1 + (i0 - 1) * 5, k2 + i2).Value = a(i3)
Next i2
Next i1
Cells(k1 + (i0 - 1) * 5, k2 + 1).Value = "Plane 1" + CStr(i0)
Next i0

Return

'   Print results (3d)

760 n2 = n2 + 1
If n2 = 3 Then
n2 = 1: k1 = k1 + 29: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 17
End If

For i0 = 1 To 4
i3 = (4 - i0) * 16
For i1 = 1 To 4
For i2 = 1 To 4
i3 = i3 + 1
Cells(k1 + 1 + (i1 - 1) * 2 + (i0 - 1) * 7, k2 + 7 + (i2 - 1) * 3 - (i1 - 1) * 2).Value = a(i3)
Next i2
Next i1
Next i0

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
```

'
' Complete format first Cube
'

```Sub Macro1()

y = MsgBox("Locked", vbCritical, "Routine Macro1, Borders")
End

For i0 = 0 To 3
For i1 = 3 To 17 Step 7
For i2 = 9 To 15 Step 3
Range(Cells(i1 + i0 * 2, i2 - i0 * 2), Cells(i1 + i0 * 2 + 6, i2 - i0 * 2 + 2)).Select: GoSub 250
Next i2
Next i1
Next i0

For i0 = 0 To 3
For i1 = 3 To 8
For i2 = 0 To 3
Range(Cells(i1 + i0 * 7, 11 - i1 + i2 * 3), Cells(i1 + i0 * 7, 11 - i1 + i2 * 3)).Select: GoSub 260
Next i2
Next i1
Next i0

Range("A1").Select

End

' Border

250 With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Color = -11489280
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Color = -11489280
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Color = -11489280
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Color = -11489280
.TintAndShade = 0
.Weight = xlThin
End With
Return

'   Diagonal

260 With Selection.Borders(xlDiagonalUp)
.LineStyle = xlContinuous
.Color = -11489280
.TintAndShade = 0
.Weight = xlThin
End With
Return

End Sub
```

'
' Format all Cubes
'

```Sub Macro2()

y = MsgBox("Locked", vbCritical, "Routine Macro2, Format all Cubes")
End

k1 = 2: k2 = 2: n2 = 1: n9 = 4

Range(Cells(2, 2), Cells(29, 17)).Select: Selection.Copy

For i1 = 2 To n9

n2 = n2 + 1
If n2 = 3 Then
n2 = 1: k1 = k1 + 29: k2 = 2
Else
k2 = k2 + 17
End If

Range(Cells(k1, k2), Cells(k1, k2)).Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

Next i1

Application.CutCopyMode = False
Range("A1").Select

End Sub

```

'
' Make Bitmaps (MSO Diagonal Format not transferable to HTML)
'

```Sub Macro3()

y = MsgBox("Locked", vbCritical, "Routine Macro3, Make Bitmaps")
End

k1 = 2: k2 = 2: n2 = 0: n9 = 4
sht1\$ = "Klad1"

For i1 = 1 To n9

n2 = n2 + 1
If n2 = 3 Then
n2 = 1: k1 = k1 + 29: k2 = 2
Else
If i1 > 1 Then k2 = k2 + 17
End If

Worksheets(sht1\$).Range(Cells(k1, k2), Cells(k1 + 28, k2 + 16)).CopyPicture xlScreen, xlBitmap
Worksheets(sht1\$).Paste Destination:=Worksheets(sht1\$).Range(Cells(k1, k2), Cells(k1, k2))

Next i1

Range("A1").Select

End Sub
```

 About the Author