Array manipulation

Lavina

Board Regular
Joined
Dec 18, 2018
Messages
75
Hello guys,

I'm trying to take a range from each sheet and add it into array.
The selected ranges will always be the same size so only the first dimension of the array needs to be redimmed

You can use:

Code:
arrayInQuestion = Range("M1:M30").Value
To set an array to be equal to a range, but after that im stuck, if i understand correctly its not possible anymore to add a new range into the array?

I have to add it into a second array and then add them together?

Or is it maybe better to select all of the fields i need and only then push them into an array?
 
Last edited:

Some videos you may like

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

NdNoviceHlp

Well-known Member
Joined
Nov 9, 2002
Messages
2,484
This will load the range from all sheets to the array...
Code:
Sub LoadShtArr()
Dim Ar() As Variant, Rng As Range, sht As Worksheet
Dim Cnt As Integer, Cnt2 As Integer, Cnt3 As Integer
Cnt = 0
Cnt2 = 0
For Each sht In ThisWorkbook.Sheets
Cnt = Cnt + 1
ReDim Preserve Ar(Cnt)
With Sheets(sht.Name)
Set Rng = .Range("M1:M30")
End With
Ar(Cnt2) = Rng
Cnt2 = Cnt2 + 1
Next sht
'insert array into sheet1 A1:A30; B1:B30; etc.
'For Cnt3 = LBound(Ar) To UBound(Ar)
'With ThisWorkbook.Sheets("sheet1")
'.Range(.Cells(1, Cnt3 + 1), .Cells(30, Cnt3 + 1)) = Ar(Cnt3)
'End With
'Next Cnt3
End Sub
U didn't say what/where U wanted the output. The code (if U uncomment the relevant code) will place the array content in sheet1 A1 to whatever. Adjust to suit. If you have more ranges that U are interested in from each sheet, it seems better just to create a new array for each range and follow the code outlined. HTH. Dave
 

Lavina

Board Regular
Joined
Dec 18, 2018
Messages
75
Thanks a lot Dave, that is some great code that does exactly what i needed! I ended up cycling through each page and adding 10 variables at a time, this will be a great improvement!
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,096,458
Messages
5,450,573
Members
405,619
Latest member
mjh237

This Week's Hot Topics

Top