Results 1 to 7 of 7

Thread: [RESOLVED] Help with a For loop

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    250

    Resolved [RESOLVED] Help with a For loop

    Hi everyone,

    I have data in a listbox, how do I loop through the data in the listbox and do a message box of each line of data in that listbox?


    data in the listbox:
    Frank
    Bill
    Jill


    How do I code a button to loop through the listbox and give a message box of each item in the listbox?

    For example, a message box should pop up for each person, Frank, Bill, and Jill.

  2. #2
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Help with a For loop

    Quote Originally Posted by Giraffe Frenzy
    Hi everyone,

    I have data in a listbox, how do I loop through the data in the listbox and do a message box of each line of data in that listbox?

    Try this:

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim a As Long
    3.    
    4.     With List1
    5.         For a = 0 to .ListCount
    6.             MsgBox .List(a), vbInformation, "Listbox Printout"
    7.         Next a
    8.    
    9.     End With
    10. End Sub
    11.  
    12. Private Sub Form_Load()
    13. Dim a As Long
    14.  
    15.     For a = 1 To 10 'Add some items
    16.         List1.AddItem "Item " & a
    17.     Next a
    18. End Sub
    Last edited by Mark Gambo; Mar 19th, 2007 at 01:06 PM.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  3. #3

  4. #4
    Lively Member
    Join Date
    Jan 2007
    Location
    Netherlands
    Posts
    80

    Re: Help with a For loop

    Do you want to display messageboxes for each item? For what purpose?

  5. #5
    Lively Member
    Join Date
    Jan 2007
    Location
    Netherlands
    Posts
    80

    Re: Help with a For loop

    or:

    Code:
    Private Sub Command1_Click()
    Dim x As Integer, i As Integer
        For i = 0 To List1.ListCount - 1
            x = MsgBox(List1.List(i), vbOKCancel, "is this what you want?")
        Next i
    End Sub
    
    Private Sub Form_Load()
        List1.AddItem "Item A"
        List1.AddItem "Item B"
        List1.AddItem "Item C"
        List1.AddItem "Item D"
        List1.AddItem "Item E"
    End Sub

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    250

    Re: Help with a For loop

    Thanks guy. That is exactly what I needed!

  7. #7
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Help with a For loop

    Quote Originally Posted by Ethan37
    or:

    Code:
    Private Sub Command1_Click()
    Dim x As Integer, i As Integer
        For i = 0 To List1.ListCount - 1
            x = MsgBox(List1.List(i), vbOKCancel, "is this what you want?")
        Next i
    End Sub
    
    Private Sub Form_Load()
        List1.AddItem "Item A"
        List1.AddItem "Item B"
        List1.AddItem "Item C"
        List1.AddItem "Item D"
        List1.AddItem "Item E"
    End Sub
    Extending on this a little (cause I'm bored)

    vb Code:
    1. Private sub command1_Click()
    2.     Dim msgRet as vbmsgboxresult, i as integer
    3.  
    4.     for i = 0 to List1.Listcount - 1
    5.         msgRet = MsgBox(list1.list(i), vbQuestion + vbYesNo, "Is this what you want?")
    6.         If msgRet = vbYes then exit for
    7.     next i
    8. End sub

Posting Permissions

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



Click Here to Expand Forum to Full Width