+ Reply to Thread
Results 1 to 7 of 7

Named range into array using VBA - Subscript out of range

  1. #1
    Registered User
    Join Date
    07-29-2018
    Location
    In the Queen's Shed
    MS-Off Ver
    2013
    Posts
    23

    Question Named range into array using VBA - Subscript out of range

    On Sheet1 I have defined a named range called XValues consisting of cells A2 through to A11. I am trying to pass the values of the named range to a 1-dimensional array in VBA in order to do a calculation on them.

    The code below is giving me a "subscript out of range" error on the line that puts the calculation results in column E. I watched the expression MyXArray, and the values from the named range do end up in the array, however, a second dimension is added. The value in cell A3 ends up in MyXArray(2,1) for example, where I assumed it would end up in MyXArray(2).

    Why? What am I doing wrong?? Why does Excel turn the 1D array into a 2D array..?

    Please Login or Register  to view this content.
    Last edited by Pak Mariman; 12-31-2020 at 05:41 AM.

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Named range into array using VBA - Subscript out of range

    No matter how you re dimension the array, it will be 2D array once you load the data into an array from the Range.
    So, MyXArray(Counter01) needs to be something like MyXArray(Counter01, 1) or MyXArray(1, Counter01)

  3. #3
    Registered User
    Join Date
    12-31-2020
    Location
    Frankfurt
    MS-Off Ver
    mostly 2010, also Excel 4
    Posts
    2

    Re: Named range into array using VBA - Subscript out of range

    Is like jindon, he did say it correct. Excel has always ranges as 2 dimensional arrays. You have then got for your range a 1 column 2 D array

    If you want to have a 1 D array, then convert it is OK to do..
    Please Login or Register  to view this content.
    Brian Tuttle
    Rule Britannia
    God Save the Queen’s Shed


    Ref
    https://www.excelforum.com/tips-and-tutorials/758402-vba-working-with-areas-within-2d-arrays.html#post5408376
    Last edited by ArchibaldTutle; 12-31-2020 at 04:08 PM.

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Named range into array using VBA - Subscript out of range

    Pak Mariman,

    If you still want it 1D array then
    for one column with many rows
    Please Login or Register  to view this content.
    for one row with many columns
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    07-29-2018
    Location
    In the Queen's Shed
    MS-Off Ver
    2013
    Posts
    23

    Re: Named range into array using VBA - Subscript out of range

    Splendid! Exactly what I needed. Thanks!!
    Last edited by AliGW; 01-01-2021 at 06:11 AM. Reason: PLEASE don't quote unnecessarily!

  6. #6
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,830

    Re: Named range into array using VBA - Subscript out of range

    Administrative Note:

    Although we value your privacy as much you do, it could be important that members have a rough idea of your location as the solutions they offer may be affected by your locale. For instance, you might in the future post questions which are related to your regional settings.

    With this in mind, please update your profile to something more precise (something such as UK, Europe, USA, UAE, etc. will suffice).

    Thank you for helping us to help you.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Named range into array using VBA - Subscript out of range

    Quote Originally Posted by Pak Mariman View Post
    Splendid! Exactly what I needed. Thanks!!
    You are welcome and thanks for the rep.

+ 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] Array Subscript Out of Range
    By js0873 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-19-2017, 09:26 AM
  2. [SOLVED] Subscript out of Range - But Array is in different workbook
    By CRIMEDOG in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-21-2015, 02:29 PM
  3. [SOLVED] Subscript out of range - Array empty
    By sperry2565 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-09-2014, 02:19 PM
  4. [SOLVED] Sheets Array - Subscript Out Of Range
    By goss in forum Excel General
    Replies: 2
    Last Post: 11-29-2012, 12:00 PM
  5. [SOLVED] Subscript out of range on Array
    By ZiadzExcel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-26-2012, 11:35 AM
  6. Subscript out of range error while populating array from range.
    By Aussiexile in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-04-2010, 02:22 AM
  7. 1D Array Problem (Subscript Out Of Range)
    By John in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-30-2006, 02:10 PM

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