Consulting

Results 1 to 14 of 14

Thread: Get data from Listbox

  1. #1
    VBAX Newbie
    Joined
    Mar 2014
    Posts
    4
    Location

    Get data from Listbox

    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
    Last edited by Bob Phillips; 03-20-2014 at 12:06 PM. Reason: Added VBA tags

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Please use code tags:

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

  3. #3
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    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
    _______________________________________________
    Please don't cross-post without providing links to your cross-posts. We answer questions for free. Please don't waste the time of the people helping you.
    For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

    - Frosty

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    @frosty,

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

  5. #5
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    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

  6. #6
    VBAX Newbie
    Joined
    Mar 2014
    Posts
    4
    Location
    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.

  7. #7
    VBAX Newbie
    Joined
    Mar 2014
    Posts
    4
    Location
    I found the solution:
        str = ListBox1.List(0)
        ListBox1.RemoveItem(0)

  8. #8
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    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

  9. #9
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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?

  10. #10
    VBAX Newbie
    Joined
    Mar 2014
    Posts
    4
    Location
    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.

  11. #11
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    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.

  12. #12
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    @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.

  13. #13
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    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."

  14. #14
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •