Solved

Transpose data in an two dimensional array

Posted on 2011-03-22
3
243 Views
Last Modified: 2012-08-13
I know that if you use Redim Preserve, you can only change the last dimesion of the array.
So I've now got a populated array that I want to paste into an excel sheet.  The problem is that the rows and columns are transposed.
Is there an easy way (memory and speed efficient) to transpose the array's info?
I thought the following could work, but doesn't....
      Range(Cells(20, 2), Cells(25, 3)) = Transpose(Arr)
Arr is a two dimensional array Arr(2,6) and I want it to be Arr(6,2)
Thanks
JL
0
Comment
Question by:jlfourie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 35188073
Using
Range(Cells(20, 2), Cells(25, 3)) = Application.Transpose(Arr)

Open in new window

may work, but I prefer to loop and create a new array:
Function TransposeGetRows(varData) As Variant
   Dim lngRow As Long, lngCol As Long
   Dim varOut()
   ReDim varOut(1 To UBound(varData, 2) + 1, 1 To UBound(varData, 1) + 1)
   For lngRow = LBound(varData, 2) To UBound(varData, 2)
      For lngCol = LBound(varData, 1) To UBound(varData, 1)
         varOut(lngRow + 1, lngCol + 1) = varData(lngCol, lngRow)
      Next lngCol
   Next lngRow
  TransposeGetRows = varOut
End Function

Open in new window


which assumes the input array is 0 based.

0
 

Author Comment

by:jlfourie
ID: 35188167
Thanks, I just got to the same application.transpose solution and wanted to update my question.  And it works well and fast.
I might try your other option, if you think it will be much quicker (memory wise).
Regards
JL
0
 

Author Closing Comment

by:jlfourie
ID: 35188170
Thanks for a speedy response.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Vlookup - 2 criteria 6 44
I NEED KEEP THE CONDITIONAL  FORMATTING 24 28
Tricky Shapes formula part 4 4 14
Copy entire column 6 25
A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question