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:

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
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
 
Upvote 0
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:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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