If you have ever used the paste special transpose option within an Excel worksheet you will know how handy it is. The transpose option allows you copy a list, for example 10 rows with 2 columns, and turn it sideways, leaving you with 2 rows and 10 columns.
Well below is a simple function you can copy straight into any VBA module. It allows you to pass in an array of any size and passes back the ‘transposed’ array results.
Public Function TransposeArray(v As Variant) As Variant' Custom Function to Transpose a 0-based array (v)
Dim x As Long, y As Long, Xupper As Long, Yupper As Long
Dim tempArray As Variant
Xupper = UBound(v, 2) 'rows
Yupper = UBound(v, 1) 'columns
ReDim tempArray(Xupper, Yupper)
For x = 0 To Xupper
For y = 0 To Yupper
tempArray(x, y) = v(y, x)
Next y
Next x
TransposeArray = tempArray
End Function
I hope it proves useful. If you have any questions or ideas for future blog posts please let us know via the comments section.
I’d like to add a thank you to a possible future Zypher-Blog colleague Niall for the example code.
No comments:
Post a Comment
Please, no purely anonymous comments, they will be deleted; always use a name for ease of reference by other commenters.