+ Reply to Thread
Results 1 to 6 of 6

0-indexed or 1-indexed arrays?

  1. #1
    Registered User
    Join Date
    08-01-2009
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    76

    0-indexed or 1-indexed arrays?

    One (of the many things) that bugs me about VB is the seemingly random way in which arrays and array-like structures seem to be either 0-indexed or 1-indexed.

    For example, many of the native collections appear to be 1-indexed, whereas the array returned by the split function is 0-indexed.

    Is there a logic to it that I should be aware of?

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    2016 Win10
    Posts
    7,180

    Re: 0-indexed or 1-indexed arrays?

    Maybe you've hit the nail on the head there, collections are 1 based, arrays aren't

  3. #3
    Forum Contributor
    Join Date
    02-28-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    187

    Re: 0-indexed or 1-indexed arrays?

    You can force an array to become 1-indexed:

    Please Login or Register  to view this content.
    I use this if I need to loop through the array but another part of the code (i.e. cell address) must start from a '1'. Saves a lot of time having to insert '+1' all over the place.

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    2016 Win10
    Posts
    7,180

    Re: 0-indexed or 1-indexed arrays?

    You can also use option base 1. Tbh I don't really find find it an issue, loops should always check the bounds of an array anyway

  5. #5
    Registered User
    Join Date
    08-01-2009
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    76

    Re: 0-indexed or 1-indexed arrays?

    Quote Originally Posted by Kyle123 View Post
    You can also use option base 1. Tbh I don't really find find it an issue, loops should always check the bounds of an array anyway
    I've discovered by trial and error (mostly error!) that this seems to be the safest way to do things. E.g. For counter = LBound(arr) To UBound(arr)

  6. #6
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: 0-indexed or 1-indexed arrays?

    FYI code below can be used to force base0 arrays to base1. HTH

    Please Login or Register  to view this content.
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

+ 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] 2nd Nth Indexed Value
    By Old Michael in forum Excel General
    Replies: 7
    Last Post: 08-19-2013, 03:47 AM
  2. [SOLVED] Indexed count and label
    By K.J.Dub in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 06-04-2013, 12:06 PM
  3. Showing indexed content
    By altotoe in forum Excel General
    Replies: 3
    Last Post: 01-24-2012, 02:30 AM
  4. Entering value at indexed location
    By Apel in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-25-2009, 04:32 PM
  5. indexed line chart?
    By Xaver Hinterhuber in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 02-22-2005, 11:06 PM

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