View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
djExcel djExcel is offline
external usenet poster
 
Posts: 17
Default Problem with Listboxes

I have a strange problem which occurs every now and then.

I have created e.g. Form1 which has a ListBox1. Then I fill in the list with
years:

For X=2004 To 2010
Form1.ListBox1.AddItem X
If X=Year(Date) Then Form1.ListBox1.ListIndex=X-2004
Next X
Form1.Show

When user select OK button, I run a code which checks the selected year:

SelectedYear = Clng(Me.ListBox1)

Sometimes this gives me an error message if user doesn't change selection in
listbox. Although Excel shows that right year is selected it somehow is not.
If I use Form1.ListBox1.SetFocus command before I set the ListIndex property
then it works just fine. But why is it that sometimes it works and sometimes
it doesn't or am I doing something wrong here?