Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 43
Default ListBox and ListIndex

I have two Option Buttons, a ListBox, and two command buttons on a UserForm.
If the user clicks on Optionbutton1, the Listbox populates with certain data.
If the user clicks on Optionbutton2, the Listbox populates with different
data. The user then selects one of the items in the listbox and clicks
"Continue" (command button). After clicking the command button, the program
sends the user to a specific Sheet.

I am having a problem with:
When the user clicks on Optionbutton2 the Listbox populates fine, however it
does not send the user to the correct sheet after he/she clicks "Continue". I
think I am doing this right and I cannot figure out what is wrong. Any help
would be greatly appreciated. My code follows below.
_______________________

Private Sub CommandButton4_Click()
' Stores the selection from the Listbox to allow the user to continue to
the correct pipe type.

If Me.ListBox1.ListIndex = 0 = True Then
Worksheets(48).Activate
UserForm12.Hide

ElseIf Me.ListBox1.ListIndex = 1 = True Then
Worksheets(49).Activate
UserForm12.Hide

ElseIf Me.ListBox1.ListIndex = 2 = True Then
Worksheets(50).Activate
UserForm12.Hide

ElseIf Me.ListBox1.ListIndex = 3 = True Then
Worksheets(51).Activate
UserForm12.Hide

ElseIf Me.ListBox1.ListIndex = 4 = True Then
Worksheets(52).Activate
UserForm12.Hide

ElseIf Me.ListBox1.ListIndex = 5 = True Then
Worksheets(68).Activate
UserForm12.Hide

ElseIf Me.ListBox1.ListIndex = 6 = True Then
Worksheets(69).Activate
UserForm12.Hide

ElseIf Me.ListBox1.ListIndex = 7 = True Then
Worksheets(70).Activate
UserForm12.Hide

ElseIf Me.ListBox1.ListIndex = 8 = True Then
Worksheets(71).Activate
UserForm12.Hide

ElseIf Me.ListBox1.ListIndex = 9 = True Then
Worksheets(72).Activate
UserForm12.Hide
End If
End Sub
Private Sub OptionButton1_Click()
Me.ListBox1.Clear
With Me.ListBox1
.AddItem "A", Index = 0
.AddItem "B", Index = 1
.AddItem "C", Index = 2
.AddItem "D", Index = 3
.AddItem "E", Index = 4
End With

End Sub
Private Sub OptionButton2_Click()
Me.ListBox1.Clear
With Me.ListBox1
.AddItem "A", Index = 5
.AddItem "B", Index = 6
.AddItem "C", Index = 7
.AddItem "D", Index = 8
.AddItem "E", Index = 9

End With

End Sub

--
Message posted via http://www.officekb.com
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 595
Default ListBox and ListIndex

mtm4300 via OfficeKB.com wrote:
Private Sub OptionButton2_Click()
Me.ListBox1.Clear
With Me.ListBox1
.AddItem "A", Index = 5
.AddItem "B", Index = 6
.AddItem "C", Index = 7
.AddItem "D", Index = 8
.AddItem "E", Index = 9

End With

End Sub


You should be getting an error on the AddItem statements. I get "Variable
not Defined" on Index. In any event, the index you supply can't be greater
than the ListCount, which should be zero after the Clear and you should get
a run time error if you removed the "Index = " part.

The index argument of AddItem doesn't assign an index number to the entry,
it only determines where in the list the new item will be added. The
ListIndex when A is selected will still be zero despite providing and Index
argument in AddItem.

Here's how I would do it:

Private Sub CommandButton4_Click()
If Me.OptionButton1.Value Then
Worksheets(Me.ListBox1.ListIndex + 48).Activate
Else
Worksheets(Me.ListBox1.ListIndex + 68).Activate
End If
Me.Hide
End Sub


--
Dick Kusleika
MS MVP - Excel
www.dailydoseofexcel.com


End Sub


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 43
Default ListBox and ListIndex

That does it. Thank you very much!

Dick Kusleika wrote:
Private Sub OptionButton2_Click()
Me.ListBox1.Clear

[quoted text clipped - 8 lines]

End Sub


You should be getting an error on the AddItem statements. I get "Variable
not Defined" on Index. In any event, the index you supply can't be greater
than the ListCount, which should be zero after the Clear and you should get
a run time error if you removed the "Index = " part.

The index argument of AddItem doesn't assign an index number to the entry,
it only determines where in the list the new item will be added. The
ListIndex when A is selected will still be zero despite providing and Index
argument in AddItem.

Here's how I would do it:

Private Sub CommandButton4_Click()
If Me.OptionButton1.Value Then
Worksheets(Me.ListBox1.ListIndex + 48).Activate
Else
Worksheets(Me.ListBox1.ListIndex + 68).Activate
End If
Me.Hide
End Sub


--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...mming/200604/1
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
combo box and .listindex dirt Excel Programming 3 September 10th 04 03:01 AM
listbox listindex problem jacob Excel Programming 2 August 25th 04 08:46 AM
listindex Steph[_3_] Excel Programming 1 June 25th 04 06:14 PM
ListIndex Todd Huttenstine Excel Programming 2 May 13th 04 04:21 PM
listbox.value not equal to listbox.list(listbox.listindex,0) ARB Excel Programming 0 October 22nd 03 12:46 AM


All times are GMT +1. The time now is 02:37 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"