+ Reply to Thread
Results 1 to 5 of 5

Populate array from range - error

  1. #1
    Registered User
    Join Date
    02-10-2011
    Location
    Canada
    MS-Off Ver
    Excel 2019 / MS365
    Posts
    55

    Populate array from range - error

    I am trying to populate a VBA array, based on values in a range of cells. Later, I will loop through the contents of the array and use the contents to complete a task. When I hardcode the array contents, everything works as intended. However, when I use the values in a range of cells, my range shape seems to change. It's almost as if I am adding an extra dimension to the range.
    The first thing to note is that the both the LBound and UBound show 1 (I am using Option Base 1). There are 4 values, so I would expect the UBound to be 4. That may be one of the reasons that the loop fails.
    I have attached a simplified sample file which includes both methods of populating the array.
    Thank you in advance for your help.
    Attached Files Attached Files
    Last edited by pinebush; 02-16-2024 at 07:48 PM.

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,325

    Re: Populate array from range - error

    By default, the variant that is assigned the cell values is a 2-D array: the first dimension is rows, the second is columns.

    Please Login or Register  to view this content.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,871

    Re: Populate array from range - error

    When you create an array from a range, surprisingly, it creates a two dimensional array where the second dimension is 1. Who knew? So, when you loop through the range, you can't just refer to, say, vArray(i), you need to refer to vArray(i, 1)
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Registered User
    Join Date
    02-10-2011
    Location
    Canada
    MS-Off Ver
    Excel 2019 / MS365
    Posts
    55

    Re: Populate array from range - error

    Thank you for your responses. I have a better understanding about arrays, and was able to incorporate them in my file.

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,871

    Re: Populate array from range - error

    You're welcome.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Populate array with large filtered range
    By Journeyman3000 in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 08-19-2020, 01:30 AM
  2. [SOLVED] Populate array to range of cells
    By hcyeap in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 09-29-2015, 09:34 AM
  3. [SOLVED] Populate one array from another array and print new array as a range
    By Kaden265 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-07-2014, 07:52 AM
  4. [SOLVED] type mismatch error when trying to populate array
    By neek13 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-21-2013, 03:35 PM
  5. Populate a range with an array from VBA
    By par0016 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-14-2013, 05:39 PM
  6. Populate 2D-array with non-contiguous range
    By Laksefar in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-11-2013, 06:37 AM
  7. Using array variable to populate range
    By sweep in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 12-19-2008, 11:14 AM

Tags for this Thread

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