ListBox Scrolling in VBA

K

Kevin

Greetings!

In Excel 2000 I am using a ListBox on a UserForm to display the progress of a lengthy procedure to the end user. As tasks are completed, I call the ListBox.AddItem method to add items to the end of the list. The problem is that the list soon becomes longer than what the ListBox viewable area can accommodate. When this happens, Windows dutifully provides a scrollbar for the user to scroll down to see subsequently added lines.

My question is: How can I make it scroll programatically so that the last item in the list is always visible? I have searched and searched but cannot seem to find the answer. Common sense (a dangerous thing) tells me that the solution can't be as allusive as it seems.

If anyone knows how to do this, please let me know.

Thanks In Advance!
 
D

Dick Kusleika

Kevin

Add this after your AddItem line

Me.ListBox1.TopIndex = Me.Listbox1.ListCount - 1

--
Dick Kusleika
MVP - Excel
Excel Blog - Daily Dose of Excel
www.dicks-blog.com

Kevin said:
Greetings!

In Excel 2000 I am using a ListBox on a UserForm to display the progress
of a lengthy procedure to the end user. As tasks are completed, I call the
ListBox.AddItem method to add items to the end of the list. The problem is
that the list soon becomes longer than what the ListBox viewable area can
accommodate. When this happens, Windows dutifully provides a scrollbar for
the user to scroll down to see subsequently added lines.
My question is: How can I make it scroll programatically so that the last
item in the list is always visible? I have searched and searched but cannot
seem to find the answer. Common sense (a dangerous thing) tells me that the
solution can't be as allusive as it seems.
 
P

Patrick Molloy

dim index as long

after you add your items set the index to the listcount property then set
the list's listindex to this...

mylist.add something
index = mylist.listcount-1 '' zero based !
mylist.listindex = index
 
K

Kevin

Thanks for your help!
--
Kevin


Dick Kusleika said:
Kevin

Add this after your AddItem line

Me.ListBox1.TopIndex = Me.Listbox1.ListCount - 1

--
Dick Kusleika
MVP - Excel
Excel Blog - Daily Dose of Excel
www.dicks-blog.com


of a lengthy procedure to the end user. As tasks are completed, I call the
ListBox.AddItem method to add items to the end of the list. The problem is
that the list soon becomes longer than what the ListBox viewable area can
accommodate. When this happens, Windows dutifully provides a scrollbar for
the user to scroll down to see subsequently added lines.
item in the list is always visible? I have searched and searched but cannot
seem to find the answer. Common sense (a dangerous thing) tells me that the
solution can't be as allusive as it seems.
 
K

Kevin

Thanks for your help!
--
Kevin


Patrick Molloy said:
dim index as long

after you add your items set the index to the listcount property then set
the list's listindex to this...

mylist.add something
index = mylist.listcount-1 '' zero based !
mylist.listindex = index
 
Top