Consulting

Results 1 to 9 of 9

Thread: Solved: Counting items vs. UBound

  1. #1
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location

    Solved: Counting items vs. UBound

    I'm playing around with a script that lists the items from a variant and then allows the user to select which item to be used. For example:

    [vba]
    Sub Exercise()
    Dim Answer As Byte
    Dim test, results As String
    Dim mytest As Variant
    Dim i As Long

    test = "Test1; Test2; Test3"

    mytest = Split(test, "; ")

    For i = 0 To UBound(mytest)
    results = results & mytest(i) & vbCrLf
    Next

    Answer = CByte(InputBox(results))

    MsgBox mytest(Answer)
    [/vba]
    The issue I have with this script is that with a variant the first item on the list is Item 0 rather than Item 1, which I expect could be confusing when someone looks at the Input box and desires to select the "first" item on the list. Selecting "3", for example, would result in an "out of range" error.

    Is there a way to do something like the above so that the number selected matches the item order on the input box list?

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Easy:
    [VBA]test = "; Test1; Test2; Test3"
    mytest = Split(test, "; ")
    For i = 1 To UBound(mytest) [/VBA]
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Quote Originally Posted by macropod
    Easy:
    [vba]test = "; Test1; Test2; Test3"
    mytest = Split(test, "; ")
    For i = 1 To UBound(mytest) [/vba]
    Thanks! That's simple enough. I appreciate the help.

  4. #4
    VBAX Contributor
    Joined
    May 2010
    Location
    Sydney, NSW, Australia
    Posts
    170
    Location
    Quote Originally Posted by macropod
    Easy:
    [vba]test = "; Test1; Test2; Test3"
    mytest = Split(test, "; ")
    For i = 1 To UBound(mytest) [/vba]
    I would probably go for Option Base 1 at the top of the module as opposed to entering a dummy value in address zero.

    Horses for courses, I just prefer using the option base .

    Of course this could effect other parts of the code but when iterating it would be better to use For i = lbound(mytest) to ubound(mytest) then the option base change shouldn't bother the code.

  5. #5
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Quote Originally Posted by Blade Hunter
    I would probably go for Option Base 1 at the top of the module as opposed to entering a dummy value in address zero.

    Horses for courses, I just prefer using the option base .

    Of course this could effect other parts of the code but when iterating it would be better to use For i = lbound(mytest) to ubound(mytest) then the option base change shouldn't bother the code.
    Thanks.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Blade Hunter
    I would probably go for Option Base 1 at the top of the module as opposed to entering a dummy value in address zero.

    Horses for courses, I just prefer using the option base .
    Yeah but run this code in step-mode with Option Base 1 and look at myArray in the watch window

    [vba]

    Dim myArray As Variant

    myArray = Split("1,2,3,4,5", ",")
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Thanks, XLD. I plan to use macropod's original suggestion, as it is simple and straightforward, something my puny brain appreciates.

  8. #8
    VBAX Contributor
    Joined
    May 2010
    Location
    Sydney, NSW, Australia
    Posts
    170
    Location
    Quote Originally Posted by xld
    Yeah but run this code in step-mode with Option Base 1 and look at myArray in the watch window

    [vba]

    Dim myArray As Variant

    myArray = Split("1,2,3,4,5", ",")
    [/vba]

    Why does that happen? I am confused now.

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Because it does . It does mean that you have to be very careful though, which is why I never bother with Option Base, it could give me a false sense of security. I prefer to control it myself.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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