VBAで行列を作る方法
次のようなプログラミングで1,0,-1の要素で作られる3×3行列を全通り調べています。
この場合3の9乗通り調べることができます。
これを4×4や5×5行列など数を大きくして調べたいのですが、このプログラムを配列を使うなどして
簡単にできる方法を教えてください。
よろしくおねがいします。
Sub test()
Dim a As Integer '行
Dim b As Integer '列
Dim c As Integer, i As Integer, j As Integer, d As Integer, e As Integer
Dim 内積 As Integer, step As Integer
Dim f As Integer, g As Integer, h As Integer, l As Integer, m As Integer, n As Integer, k As Integer, x As Integer
Dim sum As Integer, total As Integer
Dim aa As Integer, aaa As Integer, aaaa As Integer, bb As Integer, bbb As Integer, bbbb As Integer
a = 3 '行
b = 3 '列
c = 0
内積 = 0
con = 0
sum = 0
tatal = 0
aa = 0
aaa = 0
aaaa = 0
bb = 0
bbb = 0
bbbb = 0
x = 0
For n = 0 To 2
For m = 0 To 2
For l = 0 To 2
For k = 0 To 2
For h = 0 To 2
For g = 0 To 2
For f = 0 To 2
For e = 0 To 2
For d = 0 To 2
'要素がすべて1
For i = 1 To a
For j = 1 To b
Cells(i, j) = 1
Next j
Next i
If bbbb = 1 Then
Cells(a - 2, b - 2) = 0
ElseIf bbbb = 2 Then
Cells(a - 2, b - 2) = -1
End If
If bbb = 1 Then
Cells(a - 1, b - 2) = 0
ElseIf bbb = 2 Then
Cells(a - 1, b - 2) = -1
End If
If bb = 1 Then
Cells(a, b - 2) = 0
ElseIf bb = 2 Then
Cells(a, b - 2) = -1
End If
If aaaa = 1 Then
Cells(a - 2, b - 1) = 0
ElseIf aaaa = 2 Then
Cells(a - 2, b - 1) = -1
End If
If aaa = 1 Then
Cells(a - 1, b - 1) = 0
ElseIf aaa = 2 Then
Cells(a - 1, b - 1) = -1
End If
If aa = 1 Then
Cells(a, b - 1) = 0
ElseIf aa = 2 Then
Cells(a, b - 1) = -1
End If
If total = 1 Then
Cells(a - 2, b) = 0
ElseIf total = 2 Then
Cells(a - 2, b) = -1
End If
If sum = 1 Then
Cells(a - 1, b) = 0
ElseIf sum = 2 Then
Cells(a - 1, b) = -1
End If
If con = 1 Then
Cells(a, b) = 0
ElseIf con = 2 Then
Cells(a, b) = -1
End If
con = con + 1
Next d
con = 0
sum = sum + 1
Next e
sum = 0
total = total + 1
Next f
total = 0
aa = aa + 1
Next g
aa= 0
aaa = aaa + 1
Next h
aaa = 0
aaaa = aaaa + 1
Next k
aaaa = 0
bb = bb + 1
Next l
bb = 0
bbb = bbb + 1
Next m
bbb = 0
bbbb = bbbb + 1
Next n
End Sub
補足
ありがとうございました。 dim Nyukan_name(100) as String dim Outdata_ok(100) as String dim Userid_name(200) as String dim User_id(200) as String dimはstringではなくVariantで上記のソースのように作成しているのです。 ですが、インデェクスエラーが出るのですよね。(なぜだろう) 記載を忘れていたのですが、エクセルのVBAです。