Welcome to the Excel Forum

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed.

Please Register to Remove these Ads

Please Register to Remove these Ads



Reply
  #1  
Old 06-06-2009, 02:00 PM
lyndess lyndess is offline
Registered User
 
Join Date: 07 Feb 2008
Posts: 6
lyndess is becoming part of the community
Best Way to Transfer 'Dynamic' 2D Arrays?

Please Register to Remove these Ads

I believe I've reached a conculsion on this issue, but wanted to see if others might offer some expert advice.

Background: When working with two-dimensional (2D) tables of data in VBA, a best practice is to store the data in a 2D array and then transfer that array directly to an Excel range -- and vice versa. (For more on this, see here and here -- among many other other sources.)

Problem: Sometimes, it is not possible to know the full dimenions of the data table until runtime -- and not until the array is being filled with data -- thereby creating a need to potentionally redimension the 2D array (using ReDim Preserve) multiple times as it is filled up with data. (Specifically, we often know the number of columns in the data table, but not the number of rows.) Another problem here is that only the last dimension in a multi-dimension array can be resized (while preserving the existing data), but -- in the case of a 2D array that we will transfer to an Excel range -- we usually need to resize the first dimension (the rows), not the second (last) dimension (the columns).

Proposed Solution 1: Fill up the array by "flipping" rows and columns -- i.e., making the 2nd dimension in the array the rows (rather than the columns) -- and then transpose the array before writing it to the range. (Note that this solution still only allows you to resize one dimension of the array.)

Proposed Solution 2: Instead of creating and filling up a 2D array, create an array-of-arrays (described very well here by Excel guru Tushar Mehta) and fill that up with data -- effectively allowing you to re-dimension both dimensions, as necessary. Once the array-of-arrays is filled, create a new 2D array (based on the dimensions of the array-of-arrays), transfer the data from the array-of-arrays to the new 2D array (using a loop; unfortunately, simple setting the 2D array equal to the array-of-arrays won't work), and then transfer the new 2D array to Excel in one feel swoop.

Can anyone offer comments or expert advice on proposed solution 2?

(My ultimate goal is to create a reusable function that will read a set of data from a Microsoft Project file -- with a variable number of rows and columns, depending on which parameters are passed to the function -- and then transfer that data to an Excel worksheet.)

Thanks!
Reply With Quote
  #2  
Old 06-06-2009, 03:35 PM
Leith Ross's Avatar
Leith Ross Leith Ross is offline
Forum Moderator
 
Join Date: 15 Jan 2005
Location: San Francisco, Ca
MS Office Version:2000, 2003, & read 2007
Posts: 10,256
Leith Ross Has a higher level of understanding Leith Ross Has a higher level of understanding Leith Ross Has a higher level of understanding Leith Ross Has a higher level of understanding Leith Ross Has a higher level of understanding Leith Ross Has a higher level of understanding Leith Ross Has a higher level of understanding
Send a message via AIM to Leith Ross
Re: Best Way to Transfer 'Dynamic' 2D Arrays?

Hello lyndess,

It isn't clear what question you want answered. You spend a lot of time discussing arrays but very little about the specific problems you are having with them. The problems you described are vague. You should provide code samples and/or a workbook that better illustrate your problems.
__________________
Sincerely,
Leith Ross

Remember To Do the Following....

1. Use code tags. Place [code] before the first line of code and [/code] after the last line of code.
2. Thank those who have helped you by Clicking this Icon on the Gray Title Bar

3. Please mark your post [SOLVED] if it has been answered satisfactorily.


Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
Reply With Quote


Reply

Bookmarks

Tags
array , range , vba


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Forum Jump