MrExcel Publishing
Your One Stop for Excel Tips & Solutions

userform help


Posted by jim on December 19, 2001 5:33 PM

Hi

I have a list box on a userform. When the userform loads it lists all the values, but none are seletced. Is there any way to have it select a certain value by default when it loads. Is there anyway to make it select the last selected value as well?

Thanks


Posted by Tom Dickinson on December 19, 2001 6:02 PM

Jim:
After the list box loads, put in this command using your name for the list box where I have LBx:

LBx.ListIndex = LBx.ListCount - 1

Posted by jim on December 19, 2001 6:37 PM

folowup question

Hi

I tried that and it works for a userform with 1 listbox. I tried with with another userform with 2 listbox

Private Sub UserForm_Activate()

ListBox1.ListIndex = 0
ListBox2.ListIndex = 0

End Sub

and it does highlight both values like it is supposed to, but the value from the second list box comes up as null, so it doesnt work when I activate my code with those selected. If I click each selection on the listbox then it works. Am I doing something wrong?

Thanks

Posted by jim on December 19, 2001 6:37 PM

folowup question

Hi

I tried that and it works for a userform with 1 listbox. I tried with with another userform with 2 listbox

Private Sub UserForm_Activate()

ListBox1.ListIndex = 0
ListBox2.ListIndex = 0

End Sub

and it does highlight both values like it is supposed to, but the value from the second list box comes up as null, so it doesnt work when I activate my code with those selected. If I click each selection on the listbox then it works. Am I doing something wrong?

Thanks

Posted by Tom Dickinson on December 19, 2001 7:30 PM

Re: folowup question

A couple of possibilities:

You are subroutine is the start for the userform; has the list box been loaded yet? I would put the command after the loading of the list box.

Using "0" designates the first item in a listbox. Is there a value there or is it nill? (Note that if you want the last value, use IndexCount - 1 as the count starts at #1, but the index starts at #0.)

Let me know if there is still a problem

Posted by jim on December 19, 2001 8:19 PM

Re: folowup question

I have a button that does userform1.show then on userform1 i have the listboxs, arnt they loaded with theuserform? If i put it with the listbox command what would be the syntax. I tried

sub listbox1.activate, but that didnt do anything.

thanks


Posted by Tom Dickinson on December 20, 2001 7:44 AM

Re: folowup question

Are your list boxes loaded manually (I.e. through code), or have you used the rowsource property. I did a test with rowsource on 2 boxes, and each loaded properyly, then ran the lines designating the last item as the selected item. It may be that your code has one of those errors that is easily overlooked (eg. ListBox2 where you mean to have ListBox1) which could be the problem.

Tom