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 5 years have passed since last update.

【Excel VBA】配列入れ替え(x,y)->(y,x)【関数サンプル】

Last updated at Posted at 2019-08-01

※標準機能のTransposeは上限値があるため、自作した。

VBA
'-------------------------------
' 関数呼び出し例
'-------------------------------
Private Sub sample()
    '使用例
    Dim ret: ret = TransposeArray(Range("A1:B5"))
End Sub
'-------------------------------
' 配列を入れ替える。(x,y)->(y,x)
'-------------------------------
Public Function TransposeArray(arr) As Variant

    TransposeArray = ""
    
    If Not IsArray(arr) Then GoTo OnError
    
    Dim ret() As Variant: ReDim ret(1 To UBound(arr, 2), 1 To UBound(arr, 1))
    Dim i: For i = LBound(arr, 2) To UBound(arr, 2)
        Dim j: For j = LBound(arr, 1) To UBound(arr, 1)
            ret(i, j) = arr(j, i)
        Next
    Next
    
    TransposeArray = ret()

End Function
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?