ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Problem with ListBox, List Index (https://www.excelbanter.com/excel-programming/426758-problem-listbox-list-index.html)

donwb

Problem with ListBox, List Index
 
Excel 2003
I have a 2 column, populated ListBox.
The ListBox selection routine includes the code:-
MyIndex = UserForm1.ListBox1.ListIndex
(under <<Private Sub ListBox1_MouseUp(ByVal Button As Integer, ByVal Shift
As Integer, _
ByVal X As Single, ByVal Y As Single))
and MyIndex is a Publically declared variable.

The purpose of recording the user's selection in the MyIndex variable
is so that it can be used in the UserForm Initialize process to
highlight the last selection.

The Initialize routine therefore includes the code:-
UserForm1.ListBox1.ListIndex = MyIndex
(under <<Private Sub UserForm_Initialize())

When the UserForm/ListBox code is run, I get an error
Runtime Error 380 and
<<Could not set the ListIndex Property. Invalid Property Value.

I have used this code in another project and it runs fine.
The only difference is the ListBox in the project that works
has 1 column, whereas the one that fails has 2.

Any ideas would be welcome
donwb



JLGWhiz

Problem with ListBox, List Index
 
If your ListBox is MultiSelect you can't use ListIndex. See VBA help file.

"donwb" wrote:

Excel 2003
I have a 2 column, populated ListBox.
The ListBox selection routine includes the code:-
MyIndex = UserForm1.ListBox1.ListIndex
(under <<Private Sub ListBox1_MouseUp(ByVal Button As Integer, ByVal Shift
As Integer, _
ByVal X As Single, ByVal Y As Single))
and MyIndex is a Publically declared variable.

The purpose of recording the user's selection in the MyIndex variable
is so that it can be used in the UserForm Initialize process to
highlight the last selection.

The Initialize routine therefore includes the code:-
UserForm1.ListBox1.ListIndex = MyIndex
(under <<Private Sub UserForm_Initialize())

When the UserForm/ListBox code is run, I get an error
Runtime Error 380 and
<<Could not set the ListIndex Property. Invalid Property Value.

I have used this code in another project and it runs fine.
The only difference is the ListBox in the project that works
has 1 column, whereas the one that fails has 2.

Any ideas would be welcome
donwb




donwb

Problem with ListBox, List Index
 
Thanks for the response.
It's in fact single select,
but my problem turned out to be wrong order of commands in my code.
donwb

"JLGWhiz" wrote in message
...
If your ListBox is MultiSelect you can't use ListIndex. See VBA help
file.

"donwb" wrote:

Excel 2003
I have a 2 column, populated ListBox.
The ListBox selection routine includes the code:-
MyIndex = UserForm1.ListBox1.ListIndex
(under <<Private Sub ListBox1_MouseUp(ByVal Button As Integer, ByVal
Shift
As Integer, _
ByVal X As Single, ByVal Y As Single))
and MyIndex is a Publically declared variable.

The purpose of recording the user's selection in the MyIndex variable
is so that it can be used in the UserForm Initialize process to
highlight the last selection.

The Initialize routine therefore includes the code:-
UserForm1.ListBox1.ListIndex = MyIndex
(under <<Private Sub UserForm_Initialize())

When the UserForm/ListBox code is run, I get an error
Runtime Error 380 and
<<Could not set the ListIndex Property. Invalid Property Value.

I have used this code in another project and it runs fine.
The only difference is the ListBox in the project that works
has 1 column, whereas the one that fails has 2.

Any ideas would be welcome
donwb







All times are GMT +1. The time now is 05:11 PM.

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