Results 1 to 5 of 5

Thread: [RESOLVED] listbox add items

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Resolved [RESOLVED] listbox add items

    I have listbox2 and listbox 3. All this is the multiselect.

    After select the items in the listbox2, It will added the items into listbox3. So How I can remove the items selected in the listbox2 after added the items in the listbox3?

    Code:
      
       For i = 0 To List2.ListCount - 1 'select sheet
        If List2.Selected(i) = True Then
        List3.AddItem (List2.List(i))
        
        End If
        Next

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: listbox add items

    Code:
    Private Sub Command1_Click()
    Dim i As Long
    For i = List2.ListCount - 1 To 0 Step -1 ' counts list entries
        If List2.Selected(i) = True Then ' if selected then
            List3.AddItem List2.List(i) ' add to other list
            List2.RemoveItem i ' optional remove
        End If
    Next
    End Sub

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: listbox add items

    Thank you. Hack.

    I try to pop up the messagebox if not items is selected in the list2, but it not pop up the messagebox. Why?


    If List2.ListIndex = -1 Then
    MsgBox "Please choose items in the list2"
    Exit Sub
    End If

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: listbox add items

    You need to loop through the listbox but as soon as you find one thing selected, you can get out.
    Code:
    Dim blnHasSelected As Boolean
    Dim i As Long
    
            For i = 0 To List.ListCount - 1
               If List1.Selected(i) = True Then
                  blnHasSelected = True
                  Exit For
               End If
            Next
        If blnHasSelected = False Then
           MsgBox "You have not selected anything.", vbOKOnly + vbInformation, "Select Something"
        End If

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: listbox add items

    Thank you so much

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