[VB6] 陣列轉置 (Transpose)
不知為什麼VB6竟然沒有提供轉置的函數,Office VBA都有提供了,轉置就是將陣列中的元素交換,比如A(i,j)轉置後變成A(j,i),不過昇級至.Net後就沒這個問題了,因為.Net提供相當多的功能讓程式設計者使用,記錄一下使用方式
DataArray = MatrixTranspose(DataArray)'引用方式
Public Function MatrixTranspose(arrValue As Variant) As String()
Dim i As Long, j As Long, rows As Long, column As Long
Dim ReturnValue() As String
rows = UBound(arrValue, 1)
column = UBound(arrValue, 2)
ReDim ReturnValue(1 To column, 1 To rows)
For i = LBound(arrValue) To rows
For j = LBound(arrValue) To column
ReturnValue(j, i) = arrValue(i, j)
Next
Next
MatrixTranspose = ReturnValue
ReDim DataArray(1 To j - 1, 1 To i - 1)
End Function
Dim i As Long, j As Long, rows As Long, column As Long
Dim ReturnValue() As String
rows = UBound(arrValue, 1)
column = UBound(arrValue, 2)
ReDim ReturnValue(1 To column, 1 To rows)
For i = LBound(arrValue) To rows
For j = LBound(arrValue) To column
ReturnValue(j, i) = arrValue(i, j)
Next
Next
MatrixTranspose = ReturnValue
ReDim DataArray(1 To j - 1, 1 To i - 1)
End Function
DataArray為轉換前,ReturnValue為轉換後,程式執行完後確實有達到轉置的功能
範例下載:VB6陣列轉置.rar
若有謬誤,煩請告知,新手發帖請多包涵
Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET