Sub SolveABCD()
Dim A As Integer, B As Integer
Dim C As Integer, D As Integer
Dim AB As Integer, CD As Integer
Dim ABCD As Long
Dim CD2 As Long, AB2 As Long
Dim resultRow As Integer
' A, B, C, D のすべての組み合わせをループ
For A = 1 To 9
For B = 1 To 9
If B <> A Then
AB = 10 * A + B
For C = 1 To 9
If C <> A And C <> B Then
For D = 1 To 9
If D <> A And D <> B And D <> C Then
CD = 10 * C + D
If CD > AB Then
AB2 = AB ^ 2
CD2 = CD ^ 2
ABCD = 100 * AB + CD
' 条件をチェック
If CD2 - AB2 = ABCD Then
' 結果をシートに出力
Cells(resultRow, 1).Value = AB
Cells(resultRow, 2).Value = CD
Cells(resultRow, 3).Value = A
Cells(resultRow, 4).Value = B
Cells(resultRow, 5).Value = C
Cells(resultRow, 6).Value = D
resultRow = resultRow + 1
End If
End If
End If
Next D
End If
Next C
End If
Next B
Next A