0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

配列の行列を入れ替える_VBAで

Posted at

別ファイルや別シートからデータを取得/集計して配列にまとめる時に、VBAは最後の次元しか変更できないので、行方向にデータを追加したくても、一旦列方向にデータを追加して、後から行列を入れ替える。
というのをよくやるのでそのメモ。

vba全体

Sub 行列の入替え(myArray)
'配列の列/行を入替え

Dim i As Long
Dim j As Long
Dim myArray2()

ReDim myArray2(LBound(myArray, 2) To UBound(myArray, 2), LBound(myArray, 1) To UBound(myArray, 1))

For i = LBound(myArray, 1) To UBound(myArray, 1)
    For j = LBound(myArray, 2) To UBound(myArray, 2)
        myArray2(j, i) = myArray(i, j)
    Next
Next

myArray = myArray2

End Sub

使用する時は単にcall 行列の入替え(myArray)とします。
これでそのまままとめてシートに貼り付けて使える状態にする。

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?