Merge multiple Ranges into one Array VBA

johnmpl

Board Regular
Joined
Jun 14, 2013
Messages
237
Office Version
  1. 365
Platform
  1. Windows


Greetings Mr. Excel community

This question is associated with Excel VBA code . How I can place multiple discontinuous ranges within Excel VBA array? The idea is to find an efficient method of doing , looking at what can be done without loops.

Example:

Rank 10 rows x 5 columns
Rank 7 Rows x 5 columns
Range 9 rows x 5 columns

Place in a continuous array of 26 rows by 5 columns.

The example is small, but must be efficient in the amount of resulting rows (about 10000+ rows ) .

Blessings !

 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Doing loops would likely be the fastest, otherwise:

1. Create or use a scratch worksheet, copy the ranges, paste it, send the usedrange to your array, delete the worksheet if needed.
or
2. Create or use a listbox, combobox, or listview, add your items which may require a loop, send the List contents to your array, delete the control if needed.
 
Upvote 0
Hi johnmpl
If you had some specific ( reduced size ) test data I could show you in more detail along the lines of a more simplified version of what I did here.
Excel VBA to copy and paste from horizontal to vertical format
It is not completely without looping, but in total only looping 3 – 6 times, depending on exactly what you want to do. It would also not result in a continuous Array immediately, but something very similar.
Basically you use a simple loop of three times to “capture the three 2 - D Arrays into a 3 element 1 Dimensional Array of those 2 – Dimensional Arrays using a one line “Range Value Capture Command” ( You can have in VBA an Array of Arrays ). Similarly you can paste out those 3 Array Elements “stacked” one on top of the other in a simple Loop of Three times, which sequentially pastes out those elements one at a time in A one line “Array Value to cells in a range command”.
You could then of – course, re – capture in a one line Command that output Range to a single Array.
I have had no problem doing that with Arrays of final size around 7000 columns by 40000rows
Alan
 
Last edited:
Upvote 0
Thx for the response... but I now how make your suggestions... my question is if I can do it by better way than loops... Thx again.
 
Upvote 0
Thx DocAElstein!

Let me check the link you post and answer about this... Blessings!
 
Upvote 0
Hi DocAElstein!

This is a link for a Example in Excel. Thx for your help!

[FONT=source_sans_proregular]https://mega.nz/#!lwJ0zRCJ[/FONT][FONT=source_sans_proregular]!xyK0OSNZbLolLzH153nu4nIdSFyaGrN1cmwMbfMi8Z4[/FONT]
 
Upvote 0
The Ranges are: A5:J39 , A44:J65 , A70:J89

I need to put this three ranges (in real example are more) into an array. Thx!
 
Upvote 0
Hi johnmpl

I prefer to do this initially with smaller data, then everyone can see.

So If this is your input data ( reduced size )

Using Excel 2007
[Table="width:, class:head"][tr=bgcolor:skyblue][th]Row\Col[/th][th]
A
[/th][th]
B
[/th][th]
C
[/th][th]
D
[/th][th]
E
[/th][th]
F
[/th][th]
G
[/th][th]
H
[/th][th]
I
[/th][th]
J
[/th][/tr]
[tr=bgcolor:#FFFFFF][td=bgcolor:skyblue]
1
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:skyblue]
2
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:skyblue]
3
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td]VALOR TOTAL[/td][td]
27188213​
[/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:skyblue]
4
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td]REGISTROS[/td][td]
49​
[/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:skyblue]
5
[/td][td]N.[/td][td]NOMBRE BENEFICIARIO[/td][td]NIT[/td][td]ENTIDAD[/td][td]CUENTA[/td][td]TIPO CTA[/td][td]CONCEPTO[/td][td]REFERENCIA[/td][td]VALOR[/td][td]FECHA[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:skyblue]
6
[/td][td]
1​
[/td][td]Nombre 1[/td][td]900614115[/td][td]Entidad 1[/td][td]
100001​
[/td][td]Ahorros[/td][td]PROVEE[/td][td]12554[/td][td]
171293​
[/td][td]
42276​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:skyblue]
7
[/td][td]
2​
[/td][td]Nombre 2[/td][td]900644770[/td][td]Entidad 2[/td][td]
100002​
[/td][td]Ahorros[/td][td]PROVEE[/td][td]12546[/td][td]
99760​
[/td][td]
42276​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:skyblue]
8
[/td][td]
3​
[/td][td]Nombre 3[/td][td]900216067[/td][td]Entidad 3[/td][td]
100003​
[/td][td]Corriente[/td][td]PROVEE[/td][td]12545[/td][td]
919741​
[/td][td]
42276​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:skyblue]
9
[/td][td]
4​
[/td][td]Nombre 4[/td][td]800201526[/td][td]Entidad 4[/td][td]
100004​
[/td][td]Ahorros[/td][td]PROVEE[/td][td]12540[/td][td]
99502​
[/td][td]
42276​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:skyblue]
10
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:skyblue]
11
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:skyblue]
12
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td]VALOR TOTAL[/td][td]
22524031​
[/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:skyblue]
13
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td]REGISTROS[/td][td]
49​
[/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:skyblue]
14
[/td][td]N.[/td][td]NOMBRE BENEFICIARIO[/td][td]NIT[/td][td]ENTIDAD[/td][td]CUENTA[/td][td]TIPO CTA[/td][td]CONCEPTO[/td][td]REFERENCIA[/td][td]VALOR[/td][td]FECHA[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:skyblue]
15
[/td][td]
1​
[/td][td]Nombre 14[/td][td]890301884[/td][td]Entidad 14[/td][td]
100014​
[/td][td]Corriente[/td][td]PROVEE[/td][td]12490[/td][td]
421647​
[/td][td]
42276​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:skyblue]
16
[/td][td]
2​
[/td][td]Nombre 15[/td][td]890300225[/td][td]Entidad 15[/td][td]
100015​
[/td][td]Corriente[/td][td]PROVEE[/td][td]12489[/td][td]
77200​
[/td][td]
42276​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:skyblue]
17
[/td][td]
3​
[/td][td]Nombre 16[/td][td]860053831[/td][td]Entidad 16[/td][td]
100016​
[/td][td]Corriente[/td][td]PROVEE[/td][td]12488[/td][td]
464580​
[/td][td]
42276​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:skyblue]
18
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:skyblue]
19
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:skyblue]
20
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td]VALOR TOTAL[/td][td]
6521022​
[/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:skyblue]
21
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td]REGISTROS[/td][td]
49​
[/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:skyblue]
22
[/td][td]N.[/td][td]NOMBRE BENEFICIARIO[/td][td]NIT[/td][td]ENTIDAD[/td][td]CUENTA[/td][td]TIPO CTA[/td][td]CONCEPTO[/td][td]REFERENCIA[/td][td]VALOR[/td][td]FECHA[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:skyblue]
23
[/td][td]
1​
[/td][td]Nombre 1[/td][td]900614115[/td][td]Entidad 1[/td][td]
100001​
[/td][td]Ahorros[/td][td]PROVEE[/td][td]12554[/td][td]
171293​
[/td][td]
42276​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:skyblue]
24
[/td][td]
2​
[/td][td]Nombre 2[/td][td]900644770[/td][td]Entidad 2[/td][td]
100002​
[/td][td]Ahorros[/td][td]PROVEE[/td][td]12546[/td][td]
99760​
[/td][td]
42276​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:skyblue]
25
[/td][td]
3​
[/td][td]Nombre 3[/td][td]900216067[/td][td]Entidad 3[/td][td]
100003​
[/td][td]Corriente[/td][td]PROVEE[/td][td]12545[/td][td]
919741​
[/td][td]
42276​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:skyblue]
26
[/td][td]
4​
[/td][td]Nombre 4[/td][td]800201526[/td][td]Entidad 4[/td][td]
100004​
[/td][td]Ahorros[/td][td]PROVEE[/td][td]12540[/td][td]
99502​
[/td][td]
42276​
[/td][/tr]
[/table]
[Table="width:, class:grid"][tr][td]Hoja1Small[/td][/tr][/table]

_ .. then should this be the output ?

Using Excel 2007
[Table="width:, class:head"]
[tr=bgcolor:#FFFFFF][td=bgcolor:#FFE799][/td][td=bgcolor:#FFE799][/td][td=bgcolor:#FFE799][/td][td=bgcolor:#FFE799][/td][td=bgcolor:#FFE799][/td][td=bgcolor:#FFE799][/td][td=bgcolor:#FFE799][/td][td=bgcolor:#FFE799]VALOR TOTAL[/td][td=bgcolor:#FFE799]
27188213​
[/td][td=bgcolor:#FFE799][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#FFE799][/td][td=bgcolor:#FFE799][/td][td=bgcolor:#FFE799][/td][td=bgcolor:#FFE799][/td][td=bgcolor:#FFE799][/td][td=bgcolor:#FFE799][/td][td=bgcolor:#FFE799][/td][td=bgcolor:#FFE799]REGISTROS[/td][td=bgcolor:#FFE799]
49​
[/td][td=bgcolor:#FFE799][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#FFE799]N.[/td][td=bgcolor:#FFE799]NOMBRE BENEFICIARIO[/td][td=bgcolor:#FFE799]NIT[/td][td=bgcolor:#FFE799]ENTIDAD[/td][td=bgcolor:#FFE799]CUENTA[/td][td=bgcolor:#FFE799]TIPO CTA[/td][td=bgcolor:#FFE799]CONCEPTO[/td][td=bgcolor:#FFE799]REFERENCIA[/td][td=bgcolor:#FFE799]VALOR[/td][td=bgcolor:#FFE799]FECHA[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#FFE799]
1​
[/td][td=bgcolor:#FFE799]Nombre 1[/td][td=bgcolor:#FFE799]900614115[/td][td=bgcolor:#FFE799]Entidad 1[/td][td=bgcolor:#FFE799]
100001​
[/td][td=bgcolor:#FFE799]Ahorros[/td][td=bgcolor:#FFE799]PROVEE[/td][td=bgcolor:#FFE799]12554[/td][td=bgcolor:#FFE799]
171293​
[/td][td=bgcolor:#FFE799]
42276​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#FFE799]
2​
[/td][td=bgcolor:#FFE799]Nombre 2[/td][td=bgcolor:#FFE799]900644770[/td][td=bgcolor:#FFE799]Entidad 2[/td][td=bgcolor:#FFE799]
100002​
[/td][td=bgcolor:#FFE799]Ahorros[/td][td=bgcolor:#FFE799]PROVEE[/td][td=bgcolor:#FFE799]12546[/td][td=bgcolor:#FFE799]
99760​
[/td][td=bgcolor:#FFE799]
42276​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#FFE799]
3​
[/td][td=bgcolor:#FFE799]Nombre 3[/td][td=bgcolor:#FFE799]900216067[/td][td=bgcolor:#FFE799]Entidad 3[/td][td=bgcolor:#FFE799]
100003​
[/td][td=bgcolor:#FFE799]Corriente[/td][td=bgcolor:#FFE799]PROVEE[/td][td=bgcolor:#FFE799]12545[/td][td=bgcolor:#FFE799]
919741​
[/td][td=bgcolor:#FFE799]
42276​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#FFE799]
4​
[/td][td=bgcolor:#FFE799]Nombre 4[/td][td=bgcolor:#FFE799]800201526[/td][td=bgcolor:#FFE799]Entidad 4[/td][td=bgcolor:#FFE799]
100004​
[/td][td=bgcolor:#FFE799]Ahorros[/td][td=bgcolor:#FFE799]PROVEE[/td][td=bgcolor:#FFE799]12540[/td][td=bgcolor:#FFE799]
99502​
[/td][td=bgcolor:#FFE799]
42276​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#C5E0B2][/td][td=bgcolor:#C5E0B2][/td][td=bgcolor:#C5E0B2][/td][td=bgcolor:#C5E0B2][/td][td=bgcolor:#C5E0B2][/td][td=bgcolor:#C5E0B2][/td][td=bgcolor:#C5E0B2][/td][td=bgcolor:#C5E0B2]VALOR TOTAL[/td][td=bgcolor:#C5E0B2]
22524031​
[/td][td=bgcolor:#C5E0B2][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#C5E0B2][/td][td=bgcolor:#C5E0B2][/td][td=bgcolor:#C5E0B2][/td][td=bgcolor:#C5E0B2][/td][td=bgcolor:#C5E0B2][/td][td=bgcolor:#C5E0B2][/td][td=bgcolor:#C5E0B2][/td][td=bgcolor:#C5E0B2]REGISTROS[/td][td=bgcolor:#C5E0B2]
49​
[/td][td=bgcolor:#C5E0B2][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#C5E0B2]N.[/td][td=bgcolor:#C5E0B2]NOMBRE BENEFICIARIO[/td][td=bgcolor:#C5E0B2]NIT[/td][td=bgcolor:#C5E0B2]ENTIDAD[/td][td=bgcolor:#C5E0B2]CUENTA[/td][td=bgcolor:#C5E0B2]TIPO CTA[/td][td=bgcolor:#C5E0B2]CONCEPTO[/td][td=bgcolor:#C5E0B2]REFERENCIA[/td][td=bgcolor:#C5E0B2]VALOR[/td][td=bgcolor:#C5E0B2]FECHA[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#C5E0B2]
1​
[/td][td=bgcolor:#C5E0B2]Nombre 14[/td][td=bgcolor:#C5E0B2]890301884[/td][td=bgcolor:#C5E0B2]Entidad 14[/td][td=bgcolor:#C5E0B2]
100014​
[/td][td=bgcolor:#C5E0B2]Corriente[/td][td=bgcolor:#C5E0B2]PROVEE[/td][td=bgcolor:#C5E0B2]12490[/td][td=bgcolor:#C5E0B2]
421647​
[/td][td=bgcolor:#C5E0B2]
42276​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#C5E0B2]
2​
[/td][td=bgcolor:#C5E0B2]Nombre 15[/td][td=bgcolor:#C5E0B2]890300225[/td][td=bgcolor:#C5E0B2]Entidad 15[/td][td=bgcolor:#C5E0B2]
100015​
[/td][td=bgcolor:#C5E0B2]Corriente[/td][td=bgcolor:#C5E0B2]PROVEE[/td][td=bgcolor:#C5E0B2]12489[/td][td=bgcolor:#C5E0B2]
77200​
[/td][td=bgcolor:#C5E0B2]
42276​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#C5E0B2]
3​
[/td][td=bgcolor:#C5E0B2]Nombre 16[/td][td=bgcolor:#C5E0B2]860053831[/td][td=bgcolor:#C5E0B2]Entidad 16[/td][td=bgcolor:#C5E0B2]
100016​
[/td][td=bgcolor:#C5E0B2]Corriente[/td][td=bgcolor:#C5E0B2]PROVEE[/td][td=bgcolor:#C5E0B2]12488[/td][td=bgcolor:#C5E0B2]
464580​
[/td][td=bgcolor:#C5E0B2]
42276​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#F8CBAC][/td][td=bgcolor:#F8CBAC][/td][td=bgcolor:#F8CBAC][/td][td=bgcolor:#F8CBAC][/td][td=bgcolor:#F8CBAC][/td][td=bgcolor:#F8CBAC][/td][td=bgcolor:#F8CBAC][/td][td=bgcolor:#F8CBAC]VALOR TOTAL[/td][td=bgcolor:#F8CBAC]
6521022​
[/td][td=bgcolor:#F8CBAC][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#F8CBAC][/td][td=bgcolor:#F8CBAC][/td][td=bgcolor:#F8CBAC][/td][td=bgcolor:#F8CBAC][/td][td=bgcolor:#F8CBAC][/td][td=bgcolor:#F8CBAC][/td][td=bgcolor:#F8CBAC][/td][td=bgcolor:#F8CBAC]REGISTROS[/td][td=bgcolor:#F8CBAC]
49​
[/td][td=bgcolor:#F8CBAC][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#F8CBAC]N.[/td][td=bgcolor:#F8CBAC]NOMBRE BENEFICIARIO[/td][td=bgcolor:#F8CBAC]NIT[/td][td=bgcolor:#F8CBAC]ENTIDAD[/td][td=bgcolor:#F8CBAC]CUENTA[/td][td=bgcolor:#F8CBAC]TIPO CTA[/td][td=bgcolor:#F8CBAC]CONCEPTO[/td][td=bgcolor:#F8CBAC]REFERENCIA[/td][td=bgcolor:#F8CBAC]VALOR[/td][td=bgcolor:#F8CBAC]FECHA[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#F8CBAC]
1​
[/td][td=bgcolor:#F8CBAC]Nombre 1[/td][td=bgcolor:#F8CBAC]900614115[/td][td=bgcolor:#F8CBAC]Entidad 1[/td][td=bgcolor:#F8CBAC]
100001​
[/td][td=bgcolor:#F8CBAC]Ahorros[/td][td=bgcolor:#F8CBAC]PROVEE[/td][td=bgcolor:#F8CBAC]12554[/td][td=bgcolor:#F8CBAC]
171293​
[/td][td=bgcolor:#F8CBAC]
42276​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#F8CBAC]
2​
[/td][td=bgcolor:#F8CBAC]Nombre 2[/td][td=bgcolor:#F8CBAC]900644770[/td][td=bgcolor:#F8CBAC]Entidad 2[/td][td=bgcolor:#F8CBAC]
100002​
[/td][td=bgcolor:#F8CBAC]Ahorros[/td][td=bgcolor:#F8CBAC]PROVEE[/td][td=bgcolor:#F8CBAC]12546[/td][td=bgcolor:#F8CBAC]
99760​
[/td][td=bgcolor:#F8CBAC]
42276​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#F8CBAC]
3​
[/td][td=bgcolor:#F8CBAC]Nombre 3[/td][td=bgcolor:#F8CBAC]900216067[/td][td=bgcolor:#F8CBAC]Entidad 3[/td][td=bgcolor:#F8CBAC]
100003​
[/td][td=bgcolor:#F8CBAC]Corriente[/td][td=bgcolor:#F8CBAC]PROVEE[/td][td=bgcolor:#F8CBAC]12545[/td][td=bgcolor:#F8CBAC]
919741​
[/td][td=bgcolor:#F8CBAC]
42276​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#F8CBAC]
4​
[/td][td=bgcolor:#F8CBAC]Nombre 4[/td][td=bgcolor:#F8CBAC]800201526[/td][td=bgcolor:#F8CBAC]Entidad 4[/td][td=bgcolor:#F8CBAC]
100004​
[/td][td=bgcolor:#F8CBAC]Ahorros[/td][td=bgcolor:#F8CBAC]PROVEE[/td][td=bgcolor:#F8CBAC]12540[/td][td=bgcolor:#F8CBAC]
99502​
[/td][td=bgcolor:#F8CBAC]
42276​
[/td][/tr]
[/table]
[Table="width:, class:grid"][tr][td]Output[/td][/tr][/table]

I would initially get this entire info into a 1 Dimensional Array of three Elements. Each Element would be a 2- Dimensional Array as indicated by the different colours.

Where should this Array be paste out? As I Mentioned this Array itself would not be a single 2 Dimensional Array. It would be a 1 Dimensional Array of three Elements. Each of those Elements would be a 2- Dimensional Array – three such Arrays in total. If you want that in a single 2 Dimensional Array, then it must be pasted out, possibly as Kenneth Hobson said to a Worksheet that could be scrapped after The pasted out entire range is re – captured finally to a single 2 – Dimensional Array

Do you understand?

Please try to clarify if what I suggest is what you wish.

The idea from me and Kenneth Hobson is basically the same. It is not possible to directly produce the Array without looping, ( which as Kenneth Hobson said can be very fast internally with VBA ). Initially the ranges are pasted out, one on top of the other in a spare sheet, then re – captured into a Single Array.

Alan
 
Upvote 0
Hi johnmpl

So the following code works along the lies suggested. Here briefly the sections of the code:

_1) Some initial Workbook Data is obtained

_ 2) It creates a Stack Array which is a 1 D array of Elements which are in turn 2 D Arrays corresponding to each range ( 3 in your example, but the code will do it for any number with any size ( within reason ) )

_ 3) The Elements of the 1 D Array are then pasted out to a newly created temporary sheet, “Temp” whereby each 2 D Array element is stacked one on top of the other ( which looks like the Colourful table from my post #8 )

_ 4 ) This Array is then captured back to an Array.

_ 5) Message boxes demonstrate the contents of these Arrays

Note this code will also work if your Ranges are staggered, such as this


Using Excel 2007
[Table="width:, class:grid"][tr][td]-[/td][td]
A
[/td][td]
B
[/td][td]
C
[/td][td]
D
[/td][td]
E
[/td][td]
F
[/td][td]
G
[/td][td]
H
[/td][td]
I
[/td][td]
J
[/td][td]
K
[/td][td]
L
[/td][td]
M
[/td][/tr]
[tr][td]
1
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
2
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
3
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td]VALOR TOTAL[/td][td]
27188213​
[/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
4
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td]REGISTROS[/td][td]
49​
[/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
5
[/td][td]N.[/td][td]NOMBRE BENEFICIARIO[/td][td]NIT[/td][td]ENTIDAD[/td][td]CUENTA[/td][td]TIPO CTA[/td][td]CONCEPTO[/td][td]REFERENCIA[/td][td]VALOR[/td][td]FECHA[/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
6
[/td][td]
1​
[/td][td]Nombre 1[/td][td]900614115[/td][td]Entidad 1[/td][td]
100001​
[/td][td]Ahorros[/td][td]PROVEE[/td][td]12554[/td][td]
171293​
[/td][td]
42276​
[/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
7
[/td][td]
2​
[/td][td]Nombre 2[/td][td]900644770[/td][td]Entidad 2[/td][td]
100002​
[/td][td]Ahorros[/td][td]PROVEE[/td][td]12546[/td][td]
99760​
[/td][td]
42276​
[/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
8
[/td][td]
3​
[/td][td]Nombre 3[/td][td]900216067[/td][td]Entidad 3[/td][td]
100003​
[/td][td]Corriente[/td][td]PROVEE[/td][td]12545[/td][td]
919741​
[/td][td]
42276​
[/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
9
[/td][td]
4​
[/td][td]Nombre 4[/td][td]800201526[/td][td]Entidad 4[/td][td]
100004​
[/td][td]Ahorros[/td][td]PROVEE[/td][td]12540[/td][td]
99502​
[/td][td]
42276​
[/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
10
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
11
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
12
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td]VALOR TOTAL[/td][td]
22524031​
[/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
13
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td]REGISTROS[/td][td]
49​
[/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
14
[/td][td][/td][td]N.[/td][td]NOMBRE BENEFICIARIO[/td][td]NIT[/td][td]ENTIDAD[/td][td]CUENTA[/td][td]TIPO CTA[/td][td]CONCEPTO[/td][td]REFERENCIA[/td][td]VALOR[/td][td]FECHA[/td][td][/td][td][/td][/tr]

[tr][td]
15
[/td][td][/td][td]
1​
[/td][td]Nombre 14[/td][td]890301884[/td][td]Entidad 14[/td][td]
100014​
[/td][td]Corriente[/td][td]PROVEE[/td][td]12490[/td][td]
421647​
[/td][td]
42276​
[/td][td][/td][td][/td][/tr]

[tr][td]
16
[/td][td][/td][td]
2​
[/td][td]Nombre 15[/td][td]890300225[/td][td]Entidad 15[/td][td]
100015​
[/td][td]Corriente[/td][td]PROVEE[/td][td]12489[/td][td]
77200​
[/td][td]
42276​
[/td][td][/td][td][/td][/tr]

[tr][td]
17
[/td][td][/td][td]
3​
[/td][td]Nombre 16[/td][td]860053831[/td][td]Entidad 16[/td][td]
100016​
[/td][td]Corriente[/td][td]PROVEE[/td][td]12488[/td][td]
464580​
[/td][td]
42276​
[/td][td][/td][td][/td][/tr]

[tr][td]
18
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
19
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
20
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td]VALOR TOTAL[/td][td]
6521022​
[/td][td][/td][/tr]

[tr][td]
21
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td]REGISTROS[/td][td]
49​
[/td][td][/td][/tr]

[tr][td]
22
[/td][td][/td][td][/td][td][/td][td]N.[/td][td]NOMBRE BENEFICIARIO[/td][td]NIT[/td][td]ENTIDAD[/td][td]CUENTA[/td][td]TIPO CTA[/td][td]CONCEPTO[/td][td]REFERENCIA[/td][td]VALOR[/td][td]FECHA[/td][/tr]

[tr][td]
23
[/td][td][/td][td][/td][td][/td][td]
1​
[/td][td]Nombre 1[/td][td]900614115[/td][td]Entidad 1[/td][td]
100001​
[/td][td]Ahorros[/td][td]PROVEE[/td][td]12554[/td][td]
171293​
[/td][td]
42276​
[/td][/tr]

[tr][td]
24
[/td][td][/td][td][/td][td][/td][td]
2​
[/td][td]Nombre 2[/td][td]900644770[/td][td]Entidad 2[/td][td]
100002​
[/td][td]Ahorros[/td][td]PROVEE[/td][td]12546[/td][td]
99760​
[/td][td]
42276​
[/td][/tr]

[tr][td]
25
[/td][td][/td][td][/td][td][/td][td]
3​
[/td][td]Nombre 3[/td][td]900216067[/td][td]Entidad 3[/td][td]
100003​
[/td][td]Corriente[/td][td]PROVEE[/td][td]12545[/td][td]
919741​
[/td][td]
42276​
[/td][/tr]

[tr][td]
26
[/td][td][/td][td][/td][td][/td][td]
4​
[/td][td]Nombre 4[/td][td]800201526[/td][td]Entidad 4[/td][td]
100004​
[/td][td]Ahorros[/td][td]PROVEE[/td][td]12540[/td][td]
99502​
[/td][td]
42276​
[/td][/tr]
[/table]
[Table="width:, class:grid"][tr][td]Hoja1SmallTestie[/td][/tr][/table]


_ Here the code:

Rich (BB code):
Option Explicit
Sub johnmplBigStack()
Rem 1) 'Worksheets info, Input Data
Dim WB As Workbook: Set WB = ThisWorkbook ' 'Variable gets all methods, Properties etc. of Workbooks object, which intellisense will offer us after we use .Dot
Dim wsData As Worksheet: Set wsData = WB.Worksheets("Hoja1SmallTestie")
Dim srT As Long, srS As Long, sr1 As Long, srNxt As Long 'As Long 'Variable for Range start row, search start row , first start row and found next start row of Ranges. ( Long is a Big whole Number limit (-2,147,483,648 to 2,147,483,647) If you need some sort of validation the value should only be within the range of a Byte/Integer otherwise there's no point using anything but Long.--upon/after 32-bit, Integers (Short) need converted internally anyways, so a Long is actually faster. )
Let sr1 = wsData.Cells.Find(What:="*", After:=wsData.Cells(1, 1), Lookat:=xlPart, LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlNext).Row 'Get next Row with entry anywhere for Worksheet Hoja1. Method: You start at first cell then go fowards , searching for anything ( = * ) by rows, then get the row number. Method that finds next row in sheet rather than next row for particular cell. Better to use that here as we are not sure which columns are full
Let srT = sr1 ' initially start row is first start row
        '    Dim lc As Long 'Variable for last column, assuming last column is the same for all Ranges
Dim clsr As Long 'Any column in first row of a range
        '    Let lc = wsData.Cells(sr1 + 1, Columns.Count).End(xlToLeft).Column 'The Range Object ( cell ) that is the last cell  in the row of interest has the property .End ( argument Xl to left ) appleid to it. This returns a new range ( cell ) which is that of the first Range ( cell ) with something in it "looking back" the XL spreadsheet from the last cell. Then the .Column Property is applied to return a long number equal to the column number of that cell
Rem 2) 'Make Stack Array ( 1 D array of " D Array )
Dim arrIn() As Variant ' 'Variable for Dynamic Input Array. Will become full input data for one table Will be got with .Value Property which for a Range greater than 1 cell returns Elements of a collection which are defined initially as variant by VBA. So that is why we have Array() = Variant
Dim StackChops() As Variant 'StackChops is an Array of Arrays. The number of Arrays in it is equal to the number of 2 D Ranges. It must continuously be resizes, so  must be Dynamic. Must be Variant type as that in the only Type that wil Take a data Field
Dim rngNo As Long 'Number of Range/ Count of Ranges
'Let lDB = wsData.Cells(Rows.Count, 10).End(xlUp).Row 'The Range Object ( cell ) that is the last cell  in the column of interest has the property .End ( argument Xl up ) appisd to it. This returns a new range ( cell ) which is that of the first Range ( cell ) with something in it "looking up" the XL spreadsheet from the last cell. Then the .Row Property is applied to return a long number equal to the row number of that cell
    Do While srNxt <> sr1 ' we continue until the srNxt goes past end of sheet then starts at beginning and finds initial sr1 ( as it does by the methood es use to get the next row
    Let rngNo = rngNo + 1 'Increase Range count at start of each considersation of Next Range
    Let clsr = wsData.Cells.Find(What:="*", After:=wsData.Cells(srT, 1), Lookat:=xlPart, LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlNext).Column
    Let arrIn() = wsData.Cells(srT, clsr).CurrentRegion.Value2  'CurrentRegion Property applied to a Range object returns a new range which is a "box" encompasing all connected cells to that Range Object.    The Property .Value2 applied to a Range of more than 1 cell returns a colllection ( Array )  of the undelying values all the cells in that range. VBA allows a "one liner" to then assign these value to a dynamic Array. The Elements of the collection are defined initially as variant by VBA. So that is why we had Array() = Variant.
    ReDim Preserve StackChops(1 To rngNo) 'Whils preserving Array Contents, increase size to make an additional Element for next range ( By first rang this ReDims to existing size!, but WTF)
    Let StackChops(rngNo) = arrIn() 'Next Element in Array is current Input Table
    
    Let srS = srT + UBound(StackChops(rngNo), 1) 'This gives next row after current table as start for nesxt start row search.
    Let srNxt = wsData.Cells.Find(What:="*", After:=wsData.Cells(srS, 1), Lookat:=xlPart, LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlNext).Row 'Get next Row with entry anywhere for Worksheet Hoja1.
    Let srT = srNxt 'Set range start row to next found range start
    Loop 'While srNxt <> sr1
    
Rem 3) ' Paste out stacks to tempory Sheet: if Shheet does not exist first make it
    If Not Evaluate("=ISREF('Temp'!A1)") Then 'Check to see if the sheet is there by seeing if the reference to cell A1 in that sheet doesn't exist. If it is true that it does not exist, then
    WB.Worksheets.Add(After:=WB.Worksheets("Hoja1SmallTestie")).Name = "Temp" 'Add  in this Workbook given name Temp
    Else
    ThisWorkbook.Worksheets("Temp").Move After:=ThisWorkbook.Worksheets("Hoja1SmallTestie") 'Otherwise If the sheet is there it could be anywhere so we put it after "Hoja1SmallTestie"
    Worksheets("Temp").Activate
    Worksheets("Temp").Cells.Clear 'Empty any previous Data. Important to use Clear, not ClearContents to remove all formatting also or else the Usedrange Property will not work below ##
    End If

Dim j As Long, y As Long: Let y = 1 'Loop Bound variable Count for StackArray indicies, next rung ( row for rangge Element Top left Corner )
    For j = 1 To UBound(StackChops()) 'Going through each element in stacked " D Arrays
    Worksheets("Temp").Range("A" & y & "").Resize(UBound(StackChops(j), 1), UBound(StackChops(j), 2)).Value = StackChops(j) 'A nice "One" liner - Resize selected cell to size of output Array and then the allowed VBA assignment of a collection of values to a Spreadsheet range
    Let y = y + UBound(StackChops(j), 1) 'Increase rung to next free rung
    Next j
    
Rem 4) ' Produce Final Output Array
Dim arrOut() As Variant ' Dynamic Array needed as will be assigned to Tempory Sheet Usedrange, getting the Size thereof. The "One Liner Capture" method we use will return a data Field of type Variant so we must Dimension appropriately here
Let arrOut() = Worksheets("Temp").UsedRange.Value ' ##Used range property of a Worksheet returns a Range Object that is effectivelly the "box" extending to cover all cells ever used ( changed ) in a Worksheet

Rem 5) 'Demonstrate Output Array
'5a) Stack Array
Dim strMsgBox As String, arrLine() As Variant 'String for a Message box, 1 D Array for each row
    For j = 1 To UBound(StackChops()) 'Going through each element in stacked " D Arrays
        For y = 1 To UBound(StackChops(j), 1) 'going through each row in Stack 2 D Array Element
        Let arrLine() = Application.Index(StackChops(j), y, 0) 'Slice Array out Row to give a 1 D Array Array of that row https://usefulgyaan.wordpress.com/2013/06/12/vba-trick-of-the-week-slicing-an-array-without-loop-application-index/
        Let strMsgBox = strMsgBox + Join(arrLine(), ",") & vbLf 'Build String from joing 1 D Array elemnts with a "," and a carriage return
        Next y
        MsgBox Prompt:="Stack Array element " & j & " looks like this " & vbLf & strMsgBox & ""
        Let strMsgBox = "" 'Empty string for use in next Stach Element
    Next j
    Let strMsgBox = "" 'Empty string for use in next demo
'5b) Output Array
    For y = 1 To UBound(arrOut(), 1) 'going through each row in Outpt Array
    Let arrLine() = Application.Index(arrOut(), y, 0)
    Let strMsgBox = strMsgBox + Join(arrLine(), ",") & vbLf
    Next y
MsgBox Prompt:="Output Array looks like this " & vbLf & strMsgBox & ""
End Sub

Alan

P.s.
I have actually thought of a way of doing this directly with hardly any looping and without pasting out and re-capturing in using a Temporary helper sheet
I shall possibly post that later
 
Upvote 0
Hi DocAElstein!

Let me Check this code... I'm at Work now... Blessings and thx for all!
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,315
Members
452,634
Latest member
cpostell

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