Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Value from a listbox

Status
Not open for further replies.

AgatheB

Programmer
Jan 16, 2003
36
DK
Hi,
I'm trying to get a selected value from a listbox on a userform in Excel. This is the code I'm using:

If Me.Controls("lstCustNames").ListCount > 0 Then
For Cnt = 0 To Me.lstCustomerNames.ListCount
If Me.lstCustomerNames.Items(Cnt).Selected = True Then
CustName = Me.lstCustomerNames.ListIndex(Cnt).Value
End If
Next
End If

I've also tried listindex(cnt).selected and several other options, but I keep getting errors.
Can anybody help me?
Thanks
 
Hi Carol,

It's not the value part that gives the error but the Items(Cnt).Selected
Sorry for not being specific. I want to get the value of the selected item.
Thanks, Agathe
 
I wasn't being specific either:

CustName = IstCustNames.result

(instead of all the lines of code)

Does that help?

Carol, Berlin :)
 
AgetheB,
It appears that you are trying to use properties that are not supported by the control.
This code should work for you. I am not sure if you need to use the ListCount property somewhere else or not, but it is not needed for this routine to work.
I set it up to use String variables, that may need to be changed to fit your specs, but I see you basically want the Customer name. If this does not fit what you need, please explain.
Private Sub lstCustNames_Click()
Dim CustName As String
Dim Cnt As Integer
With lstCustNames
If .ListCount > 0 Then
For Cnt = 0 To .ListCount 'Not Necessary
Next
CustName = .Text
End If
End With
Label1.Caption = CustName
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top