VBA Array - copy multiple ranges from Workbooks?

MagicalHippo

Board Regular
Joined
Oct 13, 2015
Messages
122
I am a bit confused on how to attempt this as my array knowledge is somewhat limited. Initially, I thought about creating a loop to loop through workbooks and copy the ranges....but realized the copied ranges need to be dumped into their own sheets.

How would I open multiple workbooks, run the same code on all of them, and copy range to lastrow and put it into an array? (i.e. Open WB1, copy range into array, open WB2, copy range into an array. Then paste WB1 to Sheet1, and WB2 to sheet2 etc.)

Thanks :)
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hello MagicHippo,

From what you posted, the array is an unnecessary addition to the macro. Once you have determined the range to be copied, it can then be pasted into it's corresponding worksheet.
 
Upvote 0
Hello MagicHippo,

From what you posted, the array is an unnecessary addition to the macro. Once you have determined the range to be copied, it can then be pasted into it's corresponding worksheet.

Hi Leith,

That is what I had figured, I was just challenging myself to see if we could somehow incorporate everything into an array :P
 
Upvote 0
Hello MagicalHippo,

You can store the Range values in an Array but it is a lot more work. After all, Ranges are simply 2-D arrays. You would either need to create a static array large enough to hold all the other Ranges or create a dynamic array.

The problem with a static array is you need to have some idea of the maximum array size when you create it.

Dynamic arrays only allow you to expand the last subscript of of the array. In the case of a 2-D array, that would be the columns on the worksheet. As a workaround, you could transpose the Range and resize the dynamic array. You would have to do a final transpose on the array before copying it to the worksheet.

If you really are interested in learning about dynamic arrays, I can pull some code together for you.
 
Upvote 0
Hello MagicalHippo,

You can store the Range values in an Array but it is a lot more work. After all, Ranges are simply 2-D arrays. You would either need to create a static array large enough to hold all the other Ranges or create a dynamic array.

The problem with a static array is you need to have some idea of the maximum array size when you create it.

Dynamic arrays only allow you to expand the last subscript of of the array. In the case of a 2-D array, that would be the columns on the worksheet. As a workaround, you could transpose the Range and resize the dynamic array. You would have to do a final transpose on the array before copying it to the worksheet.

If you really are interested in learning about dynamic arrays, I can pull some code together for you.

Leith,

I ended up doing this task the manual way, but for future I would love to learn more on dynamic and static arrays. I find they're my weak point in VBA programming, would love some short examples. If you don't mind!
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,720
Members
448,986
Latest member
andreguerra

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top