ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Having a hard time with listbox (https://www.excelbanter.com/excel-programming/276299-having-hard-time-listbox.html)

Thurman

Having a hard time with listbox
 
Sometimes you just get lost in the code. I have a worksheet, now rounding
about 900k in size (sad thing is it only contains 3 pages of visual
reference material and no graphics). In it I have several list boxes that
manipulate the data that I have stored in them using the additem method. I
can easily get the data of a selected item, but now I find myself needing to
know if an item is in the list box from within the VBA code.

Should I ...
A.) Create an array and search the array for the item.
B.) Use a function from within the VBA designed for this perpose and I
am not aware of
C.) Go to the local pub and think about getting a brain transplant

Thanks for any input that is positive...

Thurman



Bob Phillips[_5_]

Having a hard time with listbox
 
Thurman,

Time for the pub I think <vbg!

Seriously, perhaps you should look at it differently. Where is the data for
the listboxes coming from? Assuming that you have that data somewhere, all
you need to do is search there for an item. I tend to put any data that I
will add to a listbox on a hidden worksheet, and point the boxes at there.
Checking if it's there is then simple.

Other than that you could simply loop through the values and check,
something like

Dim i As Long
For i = 0 To ListBox1.ListCount
If ListBox1.List(i) = "Hannah" Then
MsgBox "Found in item " & i
Exit For
End If
Next


--

HTH

Bob Phillips

"Thurman" wrote in message
...
Sometimes you just get lost in the code. I have a worksheet, now rounding
about 900k in size (sad thing is it only contains 3 pages of visual
reference material and no graphics). In it I have several list boxes that
manipulate the data that I have stored in them using the additem method. I
can easily get the data of a selected item, but now I find myself needing

to
know if an item is in the list box from within the VBA code.

Should I ...
A.) Create an array and search the array for the item.
B.) Use a function from within the VBA designed for this perpose and I
am not aware of
C.) Go to the local pub and think about getting a brain transplant

Thanks for any input that is positive...

Thurman





Tom Ogilvy

Having a hard time with listbox
 
Since Bob didn't mention it, another possibility is
Listbox1.listindex
gives the location in a list where the selected item is (if you haven't set
the listbox to multiselect - Bob's is the approach for multiselect as well).
Zero is the first location.

Listbox1.Listcount
gives the number of items, so Listbox1.listcount -1 is the last item.

Beyond that, it is unclear what you mean by is in the listbox. I am not
sure how a listbox (as opposed to a combobox) could have a value that is not
in the list?

--
Regards,
Tom Ogilvy


Thurman wrote in message
...
Sometimes you just get lost in the code. I have a worksheet, now rounding
about 900k in size (sad thing is it only contains 3 pages of visual
reference material and no graphics). In it I have several list boxes that
manipulate the data that I have stored in them using the additem method. I
can easily get the data of a selected item, but now I find myself needing

to
know if an item is in the list box from within the VBA code.

Should I ...
A.) Create an array and search the array for the item.
B.) Use a function from within the VBA designed for this perpose and I
am not aware of
C.) Go to the local pub and think about getting a brain transplant

Thanks for any input that is positive...

Thurman





Thurman

Having a hard time with listbox
 
Thanks. That was better than what I was about to do...

Thurman




All times are GMT +1. The time now is 09:29 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com