+ Reply to Thread
Results 1 to 13 of 13

Filling Table From Array Results in Subscript out of Range Error

  1. #1
    Registered User
    Join Date
    02-09-2010
    Location
    California, USA
    MS-Off Ver
    Excel 2019
    Posts
    94

    Filling Table From Array Results in Subscript out of Range Error

    Hello. My code below to fill a table (ChartTable1) with the contents of an array works flawlessly so long as there is more than one row of data being moved into the table. However, if the array only contains one row of data, then I receive a subscript out of range error when attempting to fill the table with the array contents.

    The last line of code results in the error only when the array tries to fill the table with one row of data. Specifically, hovering over the error reveals UBound(v, 2) = subscript out of range.


    As the array may contain both single or multiple rows of data, I need to revise the last line of code to handle both conditions without error. Any help is much appreciated!


    Thanks!
    Please Login or Register  to view this content.

  2. #2
    Valued Forum Contributor
    Join Date
    05-15-2017
    Location
    US
    MS-Off Ver
    365
    Posts
    901

    Re: Filling Table From Array Results in Subscript out of Range Error

    Can you check your array prior to that line to handle such situations?
    Is the one row only headers? or is it truely just 1 row of data?
    If its always data, then you could write a IF condition handle that and adjust the UBound portions accordingly.

    Just a guess.. but worth a try.
    If you find the suggestion or solution helpful, please consider adding reputation to the post.

  3. #3
    Registered User
    Join Date
    02-09-2010
    Location
    California, USA
    MS-Off Ver
    Excel 2019
    Posts
    94

    Re: Filling Table From Array Results in Subscript out of Range Error

    While I do have headers, the problem occurs with one or fewer rows of actual data. The IF condition idea makes sense to try. But I'm at a loss for how the UBound portions should be adjusted to handle the case of 0-1 rows of data.

  4. #4
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Filling Table From Array Results in Subscript out of Range Error

    Untested without sample file...
    Please Login or Register  to view this content.
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  5. #5
    Valued Forum Contributor
    Join Date
    05-15-2017
    Location
    US
    MS-Off Ver
    365
    Posts
    901

    Re: Filling Table From Array Results in Subscript out of Range Error

    Quote Originally Posted by sintek View Post
    Untested without sample file...
    Please Login or Register  to view this content.
    @matt w here is what i was trying to describe in my post, looks like this would do the trick.

  6. #6
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,080

    Re: Filling Table From Array Results in Subscript out of Range Error

    Another possibility
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    02-09-2010
    Location
    California, USA
    MS-Off Ver
    Excel 2019
    Posts
    94

    Re: Filling Table From Array Results in Subscript out of Range Error

    Quote Originally Posted by cubangt View Post
    @matt w here is what i was trying to describe in my post, looks like this would do the trick.
    I tried this line of code, but it functions identical to the original code with subscript out of range error for UBound(v, 2).

  8. #8
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Filling Table From Array Results in Subscript out of Range Error

    And drumroll.....Sample File...

  9. #9
    Registered User
    Join Date
    02-09-2010
    Location
    California, USA
    MS-Off Ver
    Excel 2019
    Posts
    94

    Re: Filling Table From Array Results in Subscript out of Range Error

    Quote Originally Posted by Fluff13 View Post
    Another possibility
    Please Login or Register  to view this content.
    Thank you, Fluff13. I added "If n = m Then n = m + 1" line as you suggested and it functions as expected now. I'm not exactly clear how this works, but it does.

  10. #10
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Filling Table From Array Results in Subscript out of Range Error

    Your original code will also error if your table does not start in row 1...

  11. #11
    Registered User
    Join Date
    02-09-2010
    Location
    California, USA
    MS-Off Ver
    Excel 2019
    Posts
    94

    Re: Filling Table From Array Results in Subscript out of Range Error

    Quote Originally Posted by sintek View Post
    Your original code will also error if your table does not start in row 1...
    Thanks for that insight. Is that the header row that must start in row 1....or is it the data row that must be in row 1?

  12. #12
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Filling Table From Array Results in Subscript out of Range Error

    This should be stable...
    You know Column count as you are slicing the array in beginning...
    Please Login or Register  to view this content.

  13. #13
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,080

    Re: Filling Table From Array Results in Subscript out of Range Error

    Thank you, Fluff13. I added "If n = m Then n = m + 1" line as you suggested and it functions as expected now. I'm not exactly clear how this works, but it does.
    If you only have one row, then the Index function will convert that row into a 1D array. By adding an extra "row" to n the index will return a 2D array which is what your code needs.
    Last edited by Fluff13; 08-30-2021 at 01:02 PM.

+ 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. Copying Array Gives SubScript Out Of Range Error
    By Genus Max in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-03-2020, 05:29 PM
  2. Array with Subscript Out of Range Error
    By TxTitan in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 07-03-2019, 08:51 AM
  3. Subscript out of range Error for token array
    By Shipoopi514 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-16-2017, 09:45 AM
  4. [SOLVED] subscript out of range error when slicing array
    By chirp08 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-07-2016, 11:52 AM
  5. [SOLVED] Running an Add-In results to Run-time error '9': Subscript out of range
    By ykobure in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-27-2016, 10:52 AM
  6. Defining Array - Runtime error 9, Subscript out of range error
    By MaartenW in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-03-2012, 07:32 AM
  7. redim array error....subscript out of range
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-07-2011, 11:07 AM

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