Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

VBA練習問題回答集

Last updated at Posted at 2021-09-14

ゼロベースで学ぶ EXCEL VBA の基礎 練習問題回答

5.1.2 練習

  • 下記の図のように、上記で作成したA1からA10までのデータを読み込み用データとして利用してください。
  • かつ、B1からB10にその2乗の値が表示されるようにしてください。
512
回答例
Sub ForTest()
    Dim i, imax As Long
    imax = 10
    For i = 1 To imax
        'Cells(i, 1) = i
        Cells(i, 2) = Cells(i, 1) ^ 2
    Next i
End Sub

5.2.2 練習

下記のように先程のマトリックスの行と列を入れ替えたマトリックスを作ってみましょう。

image.png

回答例
Sub ForNestTest()
    Dim i As Long, j As Long
    Dim imax As Long, jmax As Long
    imax = 10
    jmax = 10
    For i = 1 To imax
        For j = 1 To jmax
            Cells(j, i) = (i - 1) * imax + j
        Next j
    Next i
End Sub

5.2.3 練習

下記のように1から9が並ぶ3行3列のマトリックスを縦に3回繰り返すプログラムを作成してみましょう。
図では太線で囲っていますが、これは無くてかまいません。

523
回答例
Sub ForNesTest()
    Dim i As Long, j As Long, n As Long
    Dim imaxA s Long, jmax As Long, nmax As Long
    Dim irow As Long, idelta As Long
    imax = 3
    jmax = 3
    nmax = 3
    For n = 1 To nmax
        idelta = imax * (n - 1)
        For i = 1 To imax
            irow = idelta + i
            For j = 1 To jmax
                Cells(irow, j) = (i - 1) * imax + j
            Next j
        Next i
    Next n
End Sub

6.1.7 練習

以下のような10から90まで並ぶデータをエクセルに作成します。

523

このデータを3x3のa(2,2)の配列に読み込み、Debug.Printを使って以下のような a(0,0) = 10 のフォーマットで出力させてください。

イミディエイトウィンドウ
a(0,0) = 10 
a(1,0) = 20 
a(2,0) = 30 
a(0,1) = 40 
a(1,1) = 50 
a(2,1) = 60 
a(0,2) = 70 
a(1,2) = 80 
a(2,2) = 90
回答例
Sub arrayTest()
    Dim a(2, 2) As Integer
    Dim i As Integer, j As Integer, k As Integer
    
    imin = LBound(a, 1)
    imax = UBound(a, 1)
    isize = imax - imin + 1
    
    jmin = LBound(a, 2)
    jmax = UBound(a, 2)
    
    For j = jmin To jmax
        For i = imin To imax
            k = isize * j + i + 1
            a(i, j) = Cells(k, 1)
        Next
    Next
    
    For j = jmin To jmax
        For i = imin To imax
            Debug.Print "a(" & i & "," & j & ") ="; a(i, j)
        Next
    Next
End Sub
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?