+ Reply to Thread
Results 1 to 9 of 9

Named Range to Array

  1. #1
    Registered User
    Join Date
    01-01-2005
    MS-Off Ver
    2007
    Posts
    368

    Named Range to Array

    Hi,

    I am trying to populate one dimensional array with a named range. The named range "Names" refers to just one column. When I run the code below, I get a 2 dimensional array, why?

    Please Login or Register  to view this content.
    What do I have to change to make it work?

    Regards,
    Elio fernandes
    Last edited by efernandes67; 09-21-2011 at 11:48 AM.

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Named Range to Array

    Elio,

    Whenver you're loading an array directly from a cell range, the resulting array will always be 2-dimensional. The first dimension is the number of rows, the second dimension is the number of columns, eg: the resulting array will be Array(1 to rowcount, 1 to colcount). So even with a 1-column cell range, the resulting array will be 2-dimensional, the second dimension being 1 to 1 because there's only 1 column.

    If it must be a one-dimensional array, you can use the following:
    Please Login or Register  to view this content.


    Hope that helps,
    ~tigeravatar

  3. #3
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Named Range to Array

    Please Login or Register  to view this content.
    or

    Please Login or Register  to view this content.
    Last edited by snb; 09-21-2011 at 11:45 AM.



  4. #4
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Named Range to Array

    @snb, i'm not sure what that accomplishes. If the named range "Name1" is in sheet1 cells A1:A10, the variable sn would be assigned the following string value: "=Sheet1!$A1:$A10". sn is not an array of any kind...

  5. #5
    Registered User
    Join Date
    01-01-2005
    MS-Off Ver
    2007
    Posts
    368

    Re: Named Range to Array

    When I put this code, I was trying to avoid a loop to minimize the code.

    Thanks, it works.

    Elio Fernandes

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Named Range to Array

    @tigeravatar

    Both codes I posted will result in a 1-dimensional array that contains the values of A1:A10.

  7. #7
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Named Range to Array

    @snb, I was able to get the second one to result in a 1-dimensional array successfully, so that's pretty cool. The first one just wouldn't do it, though. Ah well. As always, I learned something new again today

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Named Range to Array

    @tigeravatar

    Maybe the attachment will convince you. (see the macro in it)
    Attached Files Attached Files

  9. #9
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Named Range to Array

    @snb, that's pretty slick, I see where i was going wrong with the first one now. Thanks again

+ 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