Results 1 to 7 of 7
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Ubound starting from '1' (Word97SR2)

    This snippet is from the Word97SR2 Help (Example) text for "SynonymInfo Object" (Hi Andrew Lockton!)

    <pre>Slist = Selection.Range.SynonymInfo.SynonymList(1)
    For i = 1 To UBound(Slist)
    Msgbox Slist(i)
    Next i
    </pre>


    I note that the loop runs from one to Ubound.

    When I use a string array, I loop from zero to Ubound-1.

    Can anyone shed light on, or point to, the reason for this change of heart?

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Ubound starting from '1' (Word97SR2)

    It depends on how you dimension the array, Chris. If your array starts at zero, then that's the first element you want to address. Unless you specify otherwise, an array is usually going to default to a zero lbound.
    Charlotte

  4. #3
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Ubound starting from '1' (Word97SR2)

    It appears that the string array contained in the variant SynonymList(1) is a 1-based array rather than a zero-based array. I used a simple test to check this, using the modified code below. That is, LBound(Slist) turns out to be 1 for whatever reason, rather than zero.

    For i = LBound(Slist) To UBound(Slist)
    MsgBox i & " " & Slist(i)
    Next i

    I agree that this is a bit "whacky" - I remember having to tell myself not to be tempted into using Option Base 1, since certain inbuilt functions, like Split(), return a zero-based array regardless. Then along comes this one that appears to do just the opposite, regardless. Maybe it's done like this to keep us all on our toes. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    Alan

  5. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Ubound starting from '1' (Word97SR2)

    > I agree that this is a bit "whacky"

    Thanks. Alan; this is what i want to hear. I thought it was me.

    When I started in VBA I didn't have a problem with zero-origin and 1-origin indexing - I grew up on a bit of APL - but was initially confused about some aspects of arrays.

    Now that I feel at ease with the pan-zero origin, that seems to have been adopted, this thing leaps out of the bushes at me.
    My current guess is that there were small isolated teams of programmers, and the one who did THIS was a 1-origin based guru, who either didn't know that most buddies were zero-origin or .....

    I had a similar awakening with 2-D arrays - I was used to the first subscript being the row, the second the column (a la Fortran), but VBA says only the last subscript dimension can be ReDim'ed, so now my code reads Array(col,row). (sigh!)

  6. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Ubound starting from '1' (Word97SR2)

    > For i = LBound(Slist) To

    Alan, after lunch I realised that your reply was better than I had thought.

    There's a strong case fopr replacing all, if not most of my "For i = 0 To" with "For i = LBound(Array) To". Thanks!

  7. #6
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Ubound starting from '1' (Word97SR2)

    It wasn't mine! One of the resident gurus used it in their code, in answering one of my queries here. As soon as I saw it, me thought "What a good idea! <img src=/S/clever.gif border=0 alt=clever width=15 height=15>"

    Alan

  8. #7
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Ubound starting from '1' (Word97SR2)

    Office is messed up in that some built in things return 1-based arrays and other return 0-based arrays.
    Excel is particularly annoying,

    Get used to using 0-based because that's all there is in VB.NET.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •