Hi,
I have a loop that used to get arrays from individual worksheets and then combine and write out to one worksheet.
I'm trying to get better at dealing with arrays and avoiding repeated read/write to worksheets...sufficed to say, its still a struggle.
Goal: Take each array that populated via a loop at the Sub level and append it to a global(?) or module level array that is redim'd preserved until final iteration and then the big array is written out.
Not sure I have a great example, but here is what I am experimenting with to try to figure out. It isn't a loop which I will need to get to, but at the first stage I was just trying to figure out how to append data into the big array.
Any help showing me how to 1) get individual arrays into big one and 2) do that with a loop that gets each array is greatly appreciated!
Visually represented...
I have a loop that used to get arrays from individual worksheets and then combine and write out to one worksheet.
I'm trying to get better at dealing with arrays and avoiding repeated read/write to worksheets...sufficed to say, its still a struggle.
Goal: Take each array that populated via a loop at the Sub level and append it to a global(?) or module level array that is redim'd preserved until final iteration and then the big array is written out.
Not sure I have a great example, but here is what I am experimenting with to try to figure out. It isn't a loop which I will need to get to, but at the first stage I was just trying to figure out how to append data into the big array.
Any help showing me how to 1) get individual arrays into big one and 2) do that with a loop that gets each array is greatly appreciated!
VBA Code:
Option Explicit
Public AllArray() As Variant 'Would like this array to hold all arrays.
Sub Test()
Dim ws As Worksheet
Dim Array1, Array2, Array3 As Variant
Set ws = Worksheets("Sheet1")
'####Technically I would have a loop here something like
'####For each ws in wsnames
'####Array = ws.Range("A1").CurrentRegion.Value
'####noting that row counts are always the same in each array, so only columns need to get added
'####starting location is also always the same (A1) on each sheet the loop accesses
Array1 = ws.Range("A1").CurrentRegion.Value
Array2 = ws.Range("A8").CurrentRegion.Value
Array3 = ws.Range("A15").CurrentRegion.Value
ReDim AllArray(1 To UBound(Array1, 1), 1 To UBound(Array1, 2))
AllArray = Array1
ReDim Preserve AllArray(1 To UBound(AllArray, 1), 1 To (UBound(Array1, 2) + (UBound(Array2, 2))))
' AllArray = ? This is first place I'm lost.
End Sub
Visually represented...
Book3 | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | |||
1 | Item | Color | Size | Item | Color | Size | Item | Color | Size | Item | Color | Size | ||||||
2 | 1 | Red | M | 1 | Red | M | 1 | Pink | S | 1 | Yellow | XL | ||||||
3 | 2 | Red | XL | Array 1 | 2 | Red | XL | 2 | Red | S | 2 | Green | L | |||||
4 | 3 | Blue | S | 3 | Blue | S | 3 | Green | M | 3 | Blue | XXL | ||||||
5 | 4 | Green | S | 4 | Green | S | 4 | Orange | XL | 4 | Blue | M | ||||||
6 | 5 | Yellow | M | 5 | Yellow | M | 5 | Red | L | 5 | Blue | M | ||||||
7 | ||||||||||||||||||
8 | Item | Color | Size | Array 1 | Array 2 | Array 3 | ||||||||||||
9 | 1 | Pink | S | |||||||||||||||
10 | 2 | Red | S | Array 2 | Desired Result in one array | |||||||||||||
11 | 3 | Green | M | |||||||||||||||
12 | 4 | Orange | XL | |||||||||||||||
13 | 5 | Red | L | |||||||||||||||
14 | ||||||||||||||||||
15 | Item | Color | Size | |||||||||||||||
16 | 1 | Yellow | XL | |||||||||||||||
17 | 2 | Green | L | Array 3 | ||||||||||||||
18 | 3 | Blue | XXL | |||||||||||||||
19 | 4 | Blue | M | |||||||||||||||
20 | 5 | Blue | M | |||||||||||||||
21 | ||||||||||||||||||
Sheet1 |