PDA

View Full Version : Get data from Listbox



ErikDoe
03-20-2014, 08:01 AM
Given the listbox with a few lines.
How do I read the lines?
How do I complete the line below beginning with "str ="
The result should be the same string ad added using AddItem.
TIA
Best regards
Erik


Private Sub UserForm_Initialize()
ListBox1.AddItem "1value"
ListBox1.AddItem "2value"
ListBox1.AddItem "3value"
ListBox1.AddItem "4value"
ListBox1.AddItem "5value"
End Sub

Private Function GetValue() As String
Dim str As String
Dim idx As Integer
idx = 1
str = [*******...Code to Get one value...*******]
ListBox1.RemoveItem (idx - 1)
GetValue = str
End Function

snb
03-20-2014, 08:05 AM
Please use code tags:


Private Sub UserForm_Initialize()
ListBox1.List= array("1value","2value","3value","4value","5value")
End Sub

Frosty
03-20-2014, 08:57 AM
snb,

I don't think you're answering his question at all. You're answering an entirely different question... which would be "is there another way to add items to a list box besides .AddItem?"

But since he asked "how do I *read* the value of a list box?" -- that's a bit different of a different question, yes?

To the OP: Can you explain everything you want to do, and when you want it to happen. Based on your GetValue function, it seems like you want to select an item and then immediately remove it? Or remove the first item in the list, regardless of what you selected? It's a bit confusing what your intent here is... some comments would help

snb
03-20-2014, 02:18 PM
@frosty,

This is a forum frosty, a forum.....

Frosty
03-20-2014, 02:27 PM
Don't take offense, snb... I'm just pointing out that your code snippet has nothing to do with the OP's stated question. ;)

This is forum where people ask questions, and other people answer those questions. It's not a forum where random questions and answers collide to produce interesting results, like particles in an accelerator, hehe. Take a bit of teasing for reading and posting a little too quickly ;)

ErikDoe
03-21-2014, 12:27 AM
Thanks for the answer.

But Frosty is right. I need to complete the line above that begins with "str =".

Yes, the function GetValue takes the first value from the listbox, removes it from the listbox and returns the value to further use.

ErikDoe
03-21-2014, 01:46 AM
I found the solution:


str = ListBox1.List(0)
ListBox1.RemoveItem(0)

snb
03-21-2014, 04:01 AM
Sub M_snb()
msgbox F_value 3
End sub

Private Function F_Value(y) As String
F_Value = Listbox1.list(y)
ListBox1.RemoveItem y
End Function

fumei
03-21-2014, 04:57 AM
snb:
1. that is not what the OP asked for.

Yes, the function GetValue takes the first value from the listbox, removes it from the listbox and returns the value to further use.
Mind you, the OP was not very clear at all.

2. msgbox F_value 3 causes a syntax error. It needs brackets for the 3.

Erik, I assume that str is declared as a public variable. BTW "str" is NOT a very good name. It would also be a very good idea to add some checking to make sure that the listbox actually contains items. You have code that is removing items. What happens if this is run and all items have been removed?

ErikDoe
03-21-2014, 06:12 AM
fumei:
Don't mind checking. The code is just an example to describe my problem. Not real code.
All I needed to know was the use of the Listbox.List(i) function. And I got it.
Now my real project, which has proper naming and checks for errors, is working well.

Frosty
03-21-2014, 06:53 AM
Erik,

just read the help about the control. Or use the object browser to see what methods/properties are available to you. You may also find .ListIndex helpful.

snb
03-21-2014, 07:04 AM
@fumei

I don't provide 'solutions', only suggestions with which the OP can tackle his/her 'problem' and/or improve his/her VBA skills. 'Helping'<>'solving'.
As you may have seen corroborated in post #10.

Frosty
03-21-2014, 07:32 AM
No offense, snb... But cryptic 'help' can also be harmful. Posting code with no explanation is like answering a question with a question. Yes, it can be helpful... But if the op goes off on an unhelpful tangent, then all you've really done is post a riddle which also needs to be solved.

I'm not trying to insult, just asking that you re-examine the strategy by which you answer questions and attempt to be helpful.

I'm a pretty experienced coder, and I have no idea what transpired in this thread. If I don't, then odds are someone else who finds this thread in a google search won't either. Even the OP says the "solution" was knowing about the .List property, but .RemoveItem is what was needed for the desired function. I don't know how he/she got to a solution from the above.

Even in math, you can't just answer a problem with an answer-- you have to write out the proof, or the *way* you got to your solution. In the best threads, they are a kind of proof. No, you don't want to provide the solution (I'm 100% in agreement with you there), but giving a trail of breadcrumbs educates a lot more than "try this property, you may find your way to the solution on your own... Or not, no idea, really."

fumei
03-21-2014, 05:14 PM
snb, I am perplexed as to the usefulness of posting suggestions that do not really help in an OP tackling their problem. In fact your posting looks like a solution. But....it is not really. Especially as you have a syntax error which a number of people could possibly not understand, and think the error was their fault. When it would be your fault.

Question: Hi there. I need to figure how to put text in all cells in the last row of a table

Posting: to change the header text use a headerfooter object

I am purposely vastly exaggerating. Yes, for SOME people your strategy could be educational and direct them to learn new things. However, I have absolutely no doubt that for many others it would increase confusion, which I am also sure you do not wish to do. I also do not generally provide solutions, certainly not any solution that is complex and required a great deal of effort. On the other hand, very simple things are not usually a big learning moment.

Posting: Listbox lists are 0-based, that is, the FIRST item is 0. For example with a listbox having

1value
2value
3value
4value
5value

The item 1value is ListBox1.List(0). The item 3value is ListBox1.List(2).

Done. I would not really call this a "solution". It is however, an answer. And it is educational, a learning moment.