+ Reply to Thread
Results 1 to 12 of 12

Assign array

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Assign array

    Hi, I need help assigning an array.

    In the past i have used something like the following to define a multi-dimensional array

    active_array = active_ws.Range("A1", active_ws.Cells(Rows.Count, "A").End(xlUp).Offset(, 1))
    But in this case i cant use this defn/assignment. See enclosed file. I am trying to assign to an array the shaded cells. I have a for loop and the first time through i want to assign the area in red to a multidimensional array. I will do "something" with this array and then the the next time through the loop i will assign the area in purple.

    Problem i am having is assigning the shaded cells to muli-dimensional array using the type of defn above. Any suggestions?
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Need help assigning array

    Maybe this?
    Sub x()
    
    Dim r As Range, active_array
    
    For Each r In Columns(1).SpecialCells(xlCellTypeConstants).Areas
        If r.Rows.Count > 1 Then
            active_array = r.Resize(, 2)
            ' do stuff
            Erase active_array
        End If
    Next r

  3. #3
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: Need help assigning array

    Thanks for the response.

    Here is what i came up with but for some reason i am getting a type mismatch error


    active_array = ws.Range(Cell.Address(0, 0), ws.Cells(Cell.Row, Cell.End(xlDown)).End(xlDown).Offset(, 1))
    any ideas why?

  4. #4
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: Need help assigning array

    ok...i think i fixed it ............


    active_array = ws.Range(Cell.Address(0, 0), ws.Cells(Cell.Row, Cell.End(xlDown).Offset(, 1)))

    still working on it but this seems to work ..........if not i will let you guys know.

    actually the above did not work..........................below works ( i think)

    active_array = ws.Range(Cell.Address(0, 0), ws.Cells(Cell.Row, Cell.Column).End(xlDown).Offset(, 1))

    note: cell is a defined range
    Last edited by welchs101; 09-04-2011 at 09:13 PM.

  5. #5
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Need help assigning array

    Did you try StephenR's solution?
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  6. #6
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: Need help assigning array

    I have not tried that soln yet but i will later today. Had to work on something else that came up. But i will try it!

  7. #7
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: Need help assigning array

    the code from StephenR works great.......exactly what i wanted it to do. wish i had seen this before i spent so much time trying to figure it out on my own.

    thanks for the help!

    How does it know what cells to put into the array? I mean it puts exactly the cells into the array that i was looking for.......i think it has to do with this

    SpecialCells(xlCellTypeConstants).Areas


    But i am not sure...............can someone explain?

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Need help assigning array

    Hello Welchs101,

    A Range object always has at least one Area object. The Areas collection holds each Area of Range where an Area is a group of one or more contiguous cells. Each Area is itself a Range object.

    For Example:

    Range("A1") would have a single Area that contains the cell A1.

    Range("A1:A10") would be single Area that contains cells A1 through A10.

    Range("A1:A10, J2:J6") would have 2 Areas. One for cells A1:A10 and the second for cells J2:J6.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  9. #9
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: Assign array

    thanks Leith Ross.......but how did the code know correct area? I still dont understand how only the area i wanted got selected/determined?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1