ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Listindex strange problem (https://www.excelbanter.com/excel-programming/322753-listindex-strange-problem.html)

Amit Shanker[_2_]

Listindex strange problem
 
Hi,

FYI, Office 2003 on WinXP Pro, populating a userform listbox via AddItem
method.

In the listbox, even when I can plainly see that *no* item is selected, I
get a value of 0 (zero) which indicates that the first item *is* selected :
?fmchtlist.chtlistbox.listindex (this code in the immediate window, executed
when the userform is loaded, and the listbox is populated via code BUT no
item is yet selected)

Any ideas thankfully received !

Amit



Rob Bovey

Listindex strange problem
 
"Amit Shanker" wrote in message
...
FYI, Office 2003 on WinXP Pro, populating a userform listbox via AddItem
method.

In the listbox, even when I can plainly see that *no* item is selected, I
get a value of 0 (zero) which indicates that the first item *is* selected
:
?fmchtlist.chtlistbox.listindex (this code in the immediate window,
executed when the userform is loaded, and the listbox is populated via
code BUT no item is yet selected)


Hi Amit,

Is this a multi-select listbox? If so, this is the expected behavior. In
a multi select listbox the ListIndex property refers to the currently
highlighted item in the list (not necessarily the same as selected, but
rather the one surrounded by the dotted line). Unless you specifically set
the ListIndex to some other value, the first item in the list will be
highlighted after loading the listbox from VBA.

--
Rob Bovey, Excel MVP
Application Professionals
http://www.appspro.com/

* Take your Excel development skills to the next level.
* Professional Excel Development
http://www.appspro.com/Books/Books.htm



Amit Shanker[_2_]

Listindex strange problem
 
Hi Rob,

Yes, it is indeed a multiselect listbox, with a checkbox shown against each
item. If this is expected behaviour, then perhaps you could suggest a
solution to my following problem :

What I need to do is this : In this multiselect listbox, if no item is
checked, then when I click the OK button on the userform, it should simply
unload the userform and quit. How do I trap the fact that no check mark is
there against any item (so far of course I was trying to use ListIndex
= -1)?

Thanks !


"Rob Bovey" wrote in message
...
"Amit Shanker" wrote in message
...
FYI, Office 2003 on WinXP Pro, populating a userform listbox via AddItem
method.

In the listbox, even when I can plainly see that *no* item is selected, I
get a value of 0 (zero) which indicates that the first item *is* selected
:
?fmchtlist.chtlistbox.listindex (this code in the immediate window,
executed when the userform is loaded, and the listbox is populated via
code BUT no item is yet selected)


Hi Amit,

Is this a multi-select listbox? If so, this is the expected behavior.
In a multi select listbox the ListIndex property refers to the currently
highlighted item in the list (not necessarily the same as selected, but
rather the one surrounded by the dotted line). Unless you specifically set
the ListIndex to some other value, the first item in the list will be
highlighted after loading the listbox from VBA.

--
Rob Bovey, Excel MVP
Application Professionals
http://www.appspro.com/

* Take your Excel development skills to the next level.
* Professional Excel Development
http://www.appspro.com/Books/Books.htm





Rob Bovey

Listindex strange problem
 
"Amit Shanker" wrote in message
...
Yes, it is indeed a multiselect listbox, with a checkbox shown against
each item. If this is expected behaviour, then perhaps you could suggest a
solution to my following problem :

What I need to do is this : In this multiselect listbox, if no item is
checked, then when I click the OK button on the userform, it should simply
unload the userform and quit. How do I trap the fact that no check mark is
there against any item (so far of course I was trying to use ListIndex
= -1)?


Hi Amit,

In a multi-select listbox you need to loop through each item in the list
and check the Selected property of each to determine if any items are
selected. Here's an example:

Private Sub CommandButton1_Click()
Dim bSelected As Boolean
Dim lIndex As Long
For lIndex = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(lIndex) Then bSelected = True
Next lIndex
If Not bSelected Then Unload Me ''' Nothing selected.
End Sub

--
Rob Bovey, Excel MVP
Application Professionals
http://www.appspro.com/

* Take your Excel development skills to the next level.
* Professional Excel Development
http://www.appspro.com/Books/Books.htm



Amit Shanker[_2_]

Listindex strange problem
 
Works perfect, thanks Rob.


"Rob Bovey" wrote in message
...
"Amit Shanker" wrote in message
...
Yes, it is indeed a multiselect listbox, with a checkbox shown against
each item. If this is expected behaviour, then perhaps you could suggest
a solution to my following problem :

What I need to do is this : In this multiselect listbox, if no item is
checked, then when I click the OK button on the userform, it should
simply unload the userform and quit. How do I trap the fact that no check
mark is there against any item (so far of course I was trying to use
ListIndex = -1)?


Hi Amit,

In a multi-select listbox you need to loop through each item in the
list and check the Selected property of each to determine if any items are
selected. Here's an example:

Private Sub CommandButton1_Click()
Dim bSelected As Boolean
Dim lIndex As Long
For lIndex = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(lIndex) Then bSelected = True
Next lIndex
If Not bSelected Then Unload Me ''' Nothing selected.
End Sub

--
Rob Bovey, Excel MVP
Application Professionals
http://www.appspro.com/

* Take your Excel development skills to the next level.
* Professional Excel Development
http://www.appspro.com/Books/Books.htm






All times are GMT +1. The time now is 12:31 AM.

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