ListBox Value

  • Thread starter Thread starter Carrie_Loos via OfficeKB.com
  • Start date Start date
C

Carrie_Loos via OfficeKB.com

Does anybody know of a way to capture a user highlighted item out of a list
box? Is there another tool that would be more appropriate? I would like to
then take that value and do more but I am stuck on this. I can't seem to find
anything to guide me in help or the books that I have.

Thanks
Carrie
 
Does anybody know of a way to capture a user highlighted item out of a list
box? Is there another tool that would be more appropriate? I would like to
then take that value and do more but I am stuck on this. I can't seem to find
anything to guide me in help or the books that I have.

Thanks
Carrie


Could you please copy your code into the thread.
If I have a listbox from the toolbox then the name of the listbox will
return the value.

If a sheet has a listbox (named lstbox) and a button (named cmdButton)
on it then the following should give the value of the listbox in a
message when the button is pressed:

'============================
Private Sub cmdButton_Click()
On Error Resume Next
MsgBox LstBox.value
End Sub
'============================

this will do the same, as the property value can be omitted (it being
the default property of this object):
'============================
Private Sub cmdButton_Click()
On Error Resume Next
MsgBox LstBox
End Sub
'============================

hope this helps
jason
 
For a single select listbox you can use the ListBox1.Value or the
ListBox1.Selected(index) syntax, (assuming ListBox1).

Range("A1") = ListBox1.Value
For i = 1 To ListBox1.ListCount + 1
Range("A2") = ListBox1.Selected(i)
Next
 
hi
what the user select is the value.
dim s as long 'or string
s = listbox1.value
range("A1")= s

this does not apply to multiselect list boxes.

Regards
FSt1
 
This works great! I have been stuck for days! Thanks!
For a single select listbox you can use the ListBox1.Value or the
ListBox1.Selected(index) syntax, (assuming ListBox1).

Range("A1") = ListBox1.Value
For i = 1 To ListBox1.ListCount + 1
Range("A2") = ListBox1.Selected(i)
Next
Does anybody know of a way to capture a user highlighted item out of a list
box? Is there another tool that would be more appropriate? I would like to
[quoted text clipped - 3 lines]
Thanks
Carrie
 
This: For i = 1 To ListBox1.ListCount + 1

Should have been: For i = 1 To ListBox1.ListCount - 1


Carrie_Loos via OfficeKB.com said:
This works great! I have been stuck for days! Thanks!
For a single select listbox you can use the ListBox1.Value or the
ListBox1.Selected(index) syntax, (assuming ListBox1).

Range("A1") = ListBox1.Value
For i = 1 To ListBox1.ListCount + 1
Range("A2") = ListBox1.Selected(i)
Next
Does anybody know of a way to capture a user highlighted item out of a list
box? Is there another tool that would be more appropriate? I would like to
[quoted text clipped - 3 lines]
Thanks
Carrie
 
For a single select listbox you can use the ListBox1.Value or the
ListBox1.Selected(index) syntax, (assuming ListBox1).

Range("A1") = ListBox1.Value
For i = 1 To ListBox1.ListCount + 1
Range("A2") = ListBox1.Selected(i)
Next

I think for your loop to do what you intended, it would need to look like
this...

For i = 1 To ListBox1.ListCount + 1
If ListBox1.Selected(i) Then
Range("A2") = ListBox1.Selected(i)
Exit For
End If
Next

otherwise, it will report the selected status of the last item in the list.
However, you don't need to use a loop to find the item.... you can use the
ListIndex property to get the selected item's index...

Range("A2") = ListBox1.Selected(ListBox1.ListIndex)

Rick
 
Yeah, I just wanted to illustrate the use of the index number. Guess I
should be more careful about syntax and structure.
 
For a multi-select ListBox, you should be able to get the item currently
being **pointed** to (whether selected or not) using this...

ItemPointedAt = ListBox1.List(ListBox1.ListIndex)

Rick
 
Thanks for all the info. Since I am realitively new to VBA without any formal
training some simple things can be lost so I apologize if I am asking a silly
question. The only code I could get to work was the suggestion you had
earlier. This is for a MultiList Box. I have somehow botched this up. I could
not get the option button to "unload" someone gave me this clear the option
button code which causes me a couple of problems 1) The option button never
shows "clicked" and 2) I have to re-click the option button after a list box
selection to get the code to run and show the selection in cell A1. I just
can't seem to get past this portion. Any suggestions?

Private Sub OptionButton41_Click()

ListBox1.RowSource = "Classes!Customer"

Range("A1") = ListBox1.Value
For i = 1 To ListBox1.ListCount - 1
Range("A2") = ListBox1.Selected(i)
Next

Dim ctrl As Control
For Each ctrl In UserForm1.Controls
If TypeOf ctrl Is MSForms.TextBox Then
ctrl.Value = ""
ElseIf TypeOf ctrl Is MSForms.OptionButton Then
ctrl.Value = False
End If
Next ctrl


End Sub
Yeah, I just wanted to illustrate the use of the index number. Guess I
should be more careful about syntax and structure.
[quoted text clipped - 21 lines]
 
Carrie, The code I gave you was only to illustrate the syntax for the value
of a single select list box. They were the Value and the Index properties of
the list box.
To fully understand what I gave you, you should use the VBA help file by
opening the VB editor and searching for ListBox, ListIndex, Selected(forms)
and ListCount.
There is a lot of information there that will explain when you can and
cannot use certain properties of the ListBox. I would suggest that you
practice using the different methods of applying the properties on a data
base that is not for official use. The more you experiment on mock up
models, the more you will come to understand how the code works.

Carrie_Loos via OfficeKB.com said:
Thanks for all the info. Since I am realitively new to VBA without any formal
training some simple things can be lost so I apologize if I am asking a silly
question. The only code I could get to work was the suggestion you had
earlier. This is for a MultiList Box. I have somehow botched this up. I could
not get the option button to "unload" someone gave me this clear the option
button code which causes me a couple of problems 1) The option button never
shows "clicked" and 2) I have to re-click the option button after a list box
selection to get the code to run and show the selection in cell A1. I just
can't seem to get past this portion. Any suggestions?

Private Sub OptionButton41_Click()

ListBox1.RowSource = "Classes!Customer"

Range("A1") = ListBox1.Value
For i = 1 To ListBox1.ListCount - 1
Range("A2") = ListBox1.Selected(i)
Next

Dim ctrl As Control
For Each ctrl In UserForm1.Controls
If TypeOf ctrl Is MSForms.TextBox Then
ctrl.Value = ""
ElseIf TypeOf ctrl Is MSForms.OptionButton Then
ctrl.Value = False
End If
Next ctrl


End Sub
Yeah, I just wanted to illustrate the use of the index number. Guess I
should be more careful about syntax and structure.
For a single select listbox you can use the ListBox1.Value or the
ListBox1.Selected(index) syntax, (assuming ListBox1).
[quoted text clipped - 21 lines]
 
Thanks - Actually I did understand the Value and Index properties in which I
will be changing to suit what my needs are....What was going on that I did
not understand and I have since figured out was the simple context of the the
Multi-List construction.

When I created the multi-list on the userform I had several option buttons
that were created and I was trying to run the code through those. Whereas, I
should have been running the code through the ListBox option and I was not
aware and/or thinking about that it was created. It all makes sense now.

I really was confused about the placement. Again, thanks for all your help.
Each one of these exercises may be painful but bountiful for me in so many
ways. Just wanted to say how much I appreciate everyones input.
Carrie, The code I gave you was only to illustrate the syntax for the value
of a single select list box. They were the Value and the Index properties of
the list box.
To fully understand what I gave you, you should use the VBA help file by
opening the VB editor and searching for ListBox, ListIndex, Selected(forms)
and ListCount.
There is a lot of information there that will explain when you can and
cannot use certain properties of the ListBox. I would suggest that you
practice using the different methods of applying the properties on a data
base that is not for official use. The more you experiment on mock up
models, the more you will come to understand how the code works.
Thanks for all the info. Since I am realitively new to VBA without any formal
training some simple things can be lost so I apologize if I am asking a silly
[quoted text clipped - 35 lines]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top