' Generates 3 x 3 Semi Magic Squares of Squares, Six Magic Lines, c(5) = s1/3

' Tested with Office 2007 under Windows 7

```Sub SqrSqr3d()

Dim a1(161), a(9), s(9)

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

n1 = 0: n9 = 0: n10 = 0: k1 = 1: k2 = 1
m1 = 1: m3 = 0

t1 = Timer

For j100 = 2 To 1111 ''( 101 = Test Case)
Cells(1, 1).Select: Cells(1, 1).Value = j100

n10 = 0:
s1 = Sheets("Pairs3").Cells(j100, 3).Value

m2 = Sheets("Pairs3").Cells(j100, 5).Value
For j200 = 1 To m2
a1(j200) = Sheets("Pairs3").Cells(j100, j200 + 6).Value
Next j200

a(5) = Sheets("Pairs3").Cells(j100, 4).Value

For j9 = m1 To 2 * Sqr(a(5))                                          'a(9)
a(9) = j9 ^ 2
If a(9) = a(5) Then GoTo 90

For j8 = m1 To m2                                                     'a(8)
a(8) = a1(j8)
If a(8) = a(5) Or a(8) = a(9) Then GoTo 80

a(7) = s1 - a(8) - a(9)
If a(7) < m3 Then GoTo 80
a2 = Sqr(a(7)): If Int(a2) <> a2 Then GoTo 80
If a(7) = a(5) Or a(7) = a(8) Or a(7) = a(9) Then GoTo 80

For j6 = m1 To m2                                                     'a(6)
a(6) = a1(j6)
If a(6) = a(5) Or a(6) = a(7) Or a(6) = a(8) Or a(6) = a(9) Then GoTo 60

a(4) = s1 - a(5) - a(6)
If a(4) < m3 Then GoTo 60
a2 = Sqr(a(4)): If Int(a2) <> a2 Then GoTo 60
a(3) = -a(6) + a(7) + a(8)
If a(3) < m3 Then GoTo 60
a2 = Sqr(a(3)): If Int(a2) <> a2 Then GoTo 60
a(2) = s1 - a(5) - a(8)
If a(2) < m3 Then GoTo 60
a2 = Sqr(a(2)): If Int(a2) <> a2 Then GoTo 60
a(1) = a(5) + a(6) - a(7)
If a(1) < m3 Then GoTo 60
a2 = Sqr(a(1)): If Int(a2) <> a2 Then GoTo 60

'       Exclude solutions with identical numbers

GoSub 800: If fl1 = 0 Then GoTo 60

'       n9 = n9 + 1: GoSub 640 'Print results (selected numbers)
n10 = n10 + 1
n9 = n9 + 1: GoSub 650 'Print results (squares)
GoTo 100               'Print first square only

60 Next j6
80 Next j8
90 Next j9

''Sheets("Input3").Cells(j100, 2).Value = n10

100 Next j100

t2 = Timer

t10 = Str(t2 - t1) + " sec., " + Str(n9) + " Solutions"
y = MsgBox(t10, 0, "Routine SqrSqr3d")

End

'   Print results (selected numbers)

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

'   Print results (squares)

650 n1 = n1 + 1
If n1 = 5 Then
n1 = 1: k1 = k1 + 4: k2 = 1
Else
If n9 > 1 Then k2 = k2 + 4
End If

Cells(k1, k2 + 1).Select
Cells(k1, k2 + 1).Font.Color = -4165632
If n10 = 1 Then
Cells(k1, k2 + 1).Value = CStr(n10) + " (MC = " + CStr(s1) + " )"
Else
Cells(k1, k2 + 1).Value = CStr(n10)
End If

i3 = 0
For i1 = 1 To 3
For i2 = 1 To 3
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 9
a2 = a(j1)
For j2 = (1 + j1) To 9
If a2 = a(j2) Then fl1 = 0: Return
Next j2
Next j1
Return

End Sub
```