ListBox3.ListIndex = ListBox3.ListCount -- How to select the last item in a Listbox?

S

Shaka215

Hey,

I've been messing with all sorts of methods to get this to work
correctly and for some reason it just doesn't seem to want to work
correctly. All I want the code to do is to simply select the last item
visible in a listbox. I've tried all sorts of combinations of logic
but nothing is working correctly. I figured the code...

ListBox3.ListIndex = ListBox3.ListCount

Would work but it doesn't seem to want to and gives me an error
message saying "Could not set the ListIndex property. Invalid property
value." ERROR 308. Not sure if it matters but the list box has some
thing like 500 diffrent choices to choose from...I'm simply looking
for a button that will let me have the end user go to the bottom of
the list. Any help is greatly appreciated.

-Shaka215
 
C

Chip Pearson

The ListIndex property of a ListBox is 0-based, not 1-based. Therefore, the
first item in the list is ListIndex = 0, the second item is ListIndex = 1,
and so on to the last item in the list = ListCount - 1. Thus, change

ListBox3.ListIndex = ListBox3.ListCount

to

With ListBox3
If .ListCount > 0 Then
.ListIndex = .ListCount - 1
End If
End With



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)
 
S

Shaka215

The ListIndex property of a ListBox is 0-based, not 1-based. Therefore, the
first item in the list is ListIndex = 0, the second item is ListIndex = 1,
and so on to the last item in the list = ListCount - 1. Thus, change

ListBox3.ListIndex = ListBox3.ListCount

to

With ListBox3
If .ListCount > 0 Then
.ListIndex = .ListCount - 1
End If
End With

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLCwww.cpearson.com
(email address is on the web site)










- Show quoted text -

Thanks Chip! The code worked great!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top