ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Listbox text property not working correctly (https://www.excelbanter.com/excel-programming/278552-listbox-text-property-not-working-correctly.html)

Shawn[_2_]

Listbox text property not working correctly
 
I am using Excel 2002. I have created a form with 4 listboxes all
populated with For..Next loops during the Userform Initialize
procedure.

I then select an item in each listbox using listbox1.listindex = xx.

I then try to access the selected items using listbox.text and display
the results in a message box.

I only receive the correct answer for two listboxes. The other two
listboxes (not always the same two listboxes) appear to be empty. (See
example code below.)

Private Sub UserForm_Initialize()
Dim x As Integer

For x = 1 To 50
With ListBox1
.AddItem "Number " & x
End With
Next x
For x = 1 To 50
With ListBox2
.AddItem "Number " & x
End With
Next x
For x = 1 To 50
With ListBox3
.AddItem "Number " & x
End With
Next x
For x = 1 To 50
With ListBox4
.AddItem "Number " & x
End With
Next x

With UserForm1
.ListBox1.ListIndex = 30
.ListBox2.ListIndex = 31
.ListBox3.ListIndex = 32
.ListBox4.ListIndex = 33
End With

MsgBox "Box 1 = " & ListBox1.Text & Chr(13) & "Box 2 = " &
ListBox2.Text & Chr(13)
MsgBox "Box 3 = " & ListBox3.Text & Chr(13) & "Box 4 = " &
ListBox4.Text & Chr(13)

End Sub

Any help would be greatly appreciated.



------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~ View and post usenet messages directly from http://www.ExcelForum.com/


Robin Hammond

Listbox text property not working correctly
 
Try using the value or list commands instead.

msgbox(lst1.list(lst1.listindex))

appears to be the most reliable way to get a return value even if it is long
winded.

Robin Hammond
www.enhanceddatasystems.com
Check out our XspandXL add-in


"Shawn" wrote in message
...
I am using Excel 2002. I have created a form with 4 listboxes all
populated with For..Next loops during the Userform Initialize
procedure.

I then select an item in each listbox using listbox1.listindex = xx.

I then try to access the selected items using listbox.text and display
the results in a message box.

I only receive the correct answer for two listboxes. The other two
listboxes (not always the same two listboxes) appear to be empty. (See
example code below.)

Private Sub UserForm_Initialize()
Dim x As Integer

For x = 1 To 50
With ListBox1
AddItem "Number " & x
End With
Next x
For x = 1 To 50
With ListBox2
AddItem "Number " & x
End With
Next x
For x = 1 To 50
With ListBox3
AddItem "Number " & x
End With
Next x
For x = 1 To 50
With ListBox4
AddItem "Number " & x
End With
Next x

With UserForm1
ListBox1.ListIndex = 30
ListBox2.ListIndex = 31
ListBox3.ListIndex = 32
ListBox4.ListIndex = 33
End With

MsgBox "Box 1 = " & ListBox1.Text & Chr(13) & "Box 2 = " &
ListBox2.Text & Chr(13)
MsgBox "Box 3 = " & ListBox3.Text & Chr(13) & "Box 4 = " &
ListBox4.Text & Chr(13)

End Sub

Any help would be greatly appreciated.



------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~ View and post usenet messages directly from http://www.ExcelForum.com/




Shawn[_3_]

Listbox text property not working correctly
 
Thank you Robin. It works!



------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~ View and post usenet messages directly from http://www.ExcelForum.com/



All times are GMT +1. The time now is 03:39 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com