s=0 For x=1 To 10 s = s + x Next x |
Dim result1 As Integer Dim result2 As Integer Dim result3 As Integer … Dim result10 As Integer result1 = 10 result2 = 63 result3 = 82 … result10 = 98 |
s=0 For x=1 To 10 s = s + resultx Next x |
Dim result(10) As Integer |
result(1) = 10 result(2) = 63 result(3) = 82 … result(10) = 82 |
(補足) なお、C++ や Java など、他のプログラミング言語に通じている人のために触れておくと、 VBA では「Dim result(10) As Integer」という宣言で result(0), result(1), …, result(10) の 11 個のデータが利用できる。 「Dim result(9) As Integer」と宣言するようにすれば C++ や Java と同様 result(0), result(1), …, result(9) の 10 個のデータが利用できるようになる。 |
result(1) = 10: result(2) = 38: result(3) = 81: result(4) = 68 result(5) = 52: result(6) = 71: result(7) = 61: result(8) = 98 result(9) = 41: result(10) = 68 |
eng(1) = "one": eng(2) = "two": eng(3) = "three": eng(4) = "four" eng(5) = "five": eng(6) = "six": eng(7) = "seven": eng(8) = "eight" eng(9) = "nine": eng(10) = "ten" |
MsgBox x & "は英語で" & eng(x) |
' 配列を使わないと…? If x = 1 Then MsgBox x & "は英語で" & eng1 ElseIf x = 2 Then MsgBox x & "は英語で" & eng2 ElseIf x = 3 Then MsgBox x & "は英語で" & eng3 … ElseIf x = 10 Then MsgBox x & "は英語で" & eng10 End If |
' 「コンパイルエラー : 定数式が必要です」というエラーが出る Dim n As Integer n = 10 Dim x(n) As Integer ' 1〜n の配列が利用したいのだが…? |
' こちらは正しく動作する Dim n As Integer n = 10 Dim x() As Integer ReDim x(n) ' x(0) から x(n) まで利用可能 Erase x |