+ Reply to Thread
Results 1 to 10 of 10

Read Excel named range to Array

Hybrid View

  1. #1
    Registered User
    Join Date
    08-30-2011
    Location
    Sherbrooke, Qc
    MS-Off Ver
    Excel 2003
    Posts
    5

    Read Excel named range to Array

    This one is probably easy for you guys
    I have a named array of cell in Excel
    Ex. cells A1 to A10 are called "Description"
    I am trying to read this range into an array in VBA

    This way, if one line is added to Description, the code would read the new entry.

    Can anyone help ?
    thanks
    Last edited by dizabled; 09-01-2011 at 07:49 PM.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    12,997

    Re: Read Excel named range to Array

        Dim myArray As Variant
        myArray = Application.Transpose(Range("namedarray"))
    Ben Van Johnson

  3. #3
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,447

    Re: Read Excel named range to Array

    Hi,

    Why did you use Transpose when
    myArray = Application.Range("NamedArray")
    seems to work also?
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    12,997

    Re: Read Excel named range to Array

    That's the only way I know to reduce from a 2D to 1D vector/array

  5. #5
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,447

    Re: Read Excel named range to Array

    I tried the Transpose in your code on a 4x2 array and it became a 2x4 array. Still a 2 dimensional array.

    I'm not good with getting stuff into arrays so I was excited to see your code and mine below it really put cells into a VBA array. Thanks for the lesson. Now this 2d to 1d confused me.

  6. #6
    Registered User
    Join Date
    08-30-2011
    Location
    Sherbrooke, Qc
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Read Excel named range to Array

    Thanks for helping me out.

    What's with "application", what does this refer to ?
    I was trying to work around using
    workbook(Mybook).sheet(Mysheet).range(myrangename)

  7. #7
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,447

    Re: Read Excel named range to Array

    Hi dizabled and welcome to the forum.

    This sounds like a job for Dynamic Named Ranges. If you add a line to your array the DYNAMIC range gets bigger.

    See http://www.ozgrid.com/Excel/DynamicRanges.htm or
    http://www.exceluser.com/explore/dynname1.htm

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

    Re: Read Excel named range to Array

    Hi dizabled, please use code tags around code as per rule number 3 of the forum rules (located in my signature.
    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---

+ 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