Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
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?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default Problem with Listboxes

I don't know if it will cure the problem, but it might be more efficient if
you cut this:

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

from the main module and put it in the Form1 initialize event. I believe it
would then automatically have focus when you show the form.


"djExcel" wrote:

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?

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
listboxes [email protected] Excel Programming 2 March 30th 05 06:41 PM
listboxes [email protected] Excel Programming 3 February 22nd 05 04:01 PM
Problem with listboxes Kalyan Raman Excel Programming 0 November 1st 04 12:43 PM
Listboxes 2 Denise Excel Programming 2 June 8th 04 06:39 PM
listboxes Mandy[_2_] Excel Programming 3 May 7th 04 04:38 PM


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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"