Results 1 to 4 of 4

Thread: Array manipulation
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Dec 2018
    Posts
    35
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Array manipulation

    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 by Lavina; Mar 25th, 2019 at 07:31 AM.

  2. #2
    Board Regular NdNoviceHlp's Avatar
    Join Date
    Nov 2002
    Location
    Manitoba Canada
    Posts
    2,249
    Post Thanks / Like
    Mentioned
    6 Post(s)
    Tagged
    2 Thread(s)

    Default Re: Array manipulation

    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

  3. #3
    New Member
    Join Date
    Dec 2018
    Posts
    35
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Array manipulation

    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 by Lavina; Mar 25th, 2019 at 10:15 AM.

  4. #4
    Board Regular NdNoviceHlp's Avatar
    Join Date
    Nov 2002
    Location
    Manitoba Canada
    Posts
    2,249
    Post Thanks / Like
    Mentioned
    6 Post(s)
    Tagged
    2 Thread(s)

    Default Re: Array manipulation

    You are welcome. Thanks for posting your outcome. Dave

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •