PDA

View Full Version : Solved: Counting items vs. UBound



Opv
05-22-2011, 12:41 PM
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:


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)

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?

macropod
05-22-2011, 01:45 PM
Easy:
test = "; Test1; Test2; Test3"
mytest = Split(test, "; ")
For i = 1 To UBound(mytest)

Opv
05-22-2011, 01:52 PM
Easy:
test = "; Test1; Test2; Test3"
mytest = Split(test, "; ")
For i = 1 To UBound(mytest)

Thanks! That's simple enough. I appreciate the help.

Blade Hunter
05-22-2011, 09:51 PM
Easy:
test = "; Test1; Test2; Test3"
mytest = Split(test, "; ")
For i = 1 To UBound(mytest)

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.

Opv
05-23-2011, 09:28 AM
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.

Bob Phillips
05-23-2011, 10:05 AM
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



Dim myArray As Variant

myArray = Split("1,2,3,4,5", ",")

Opv
05-23-2011, 10:18 AM
Thanks, XLD. I plan to use macropod's original suggestion, as it is simple and straightforward, something my puny brain appreciates.

Blade Hunter
05-23-2011, 04:58 PM
Yeah but run this code in step-mode with Option Base 1 and look at myArray in the watch window



Dim myArray As Variant

myArray = Split("1,2,3,4,5", ",")



Why does that happen? I am confused now.

Bob Phillips
05-24-2011, 12:18 AM
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.