Add to one list and delete from another

ccordner

Active Member
Joined
Apr 28, 2010
Messages
355
I'm trying to create a standard listbox, with the option to move items from one to another.

As you can imagine, I have a button with the following code:

VBA Code:
With lstLocations

    For lngListCount = 0 To .ListCount - 1
    
        If .Selected(lngListCount) = True Then
        
            Call AddEntry("lstList1", .List(lngListCount))
            .RemoveItem (lngListCount)
        
        End If
        
    Next

End With

It checks which items are selected, adds them to the list box (using the subroutine AddEntry), but the problem comes when you remove the item - the list is now shorter, so of course it's looking for an entry that doesn't exist.

Any ideas how I can deal with this?
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Hi ccordner,

This is the solution I use in one of my userforms. The user must select a topic in one listbox, hit the Select button, and it will move it to the other list box.

VBA Code:
Private Sub addTopic_Click()
    Dim uniqueFlag As Boolean
    
    uniqueFlag = True
    
    With Me
        For x = 0 To .availableTopics.ListCount - 1
            If .availableTopics.Selected(x) Then
                For y = 0 To .selectedTopics.ListCount - 1
                    If .selectedTopics.List(y) = .availableTopics.List(x) Then
                        uniqueFlag = False
                    End If
                Next y
                
                If uniqueFlag Then
                    .selectedTopics.AddItem .availableTopics.List(x)
                End If
                
                .availableTopics.Selected(x) = False
                Call AddRemoveAttachments(.availableTopics.List(x), 1)
                
                .availableTopics.RemoveItem (x)
                Exit For
            End If
        Next x
    End With
End Sub

Private Sub removeTopic_Click()
    Dim uniqueFlag  As Boolean
    uniqueFlag = True

    With Me
        For x = 0 To .selectedTopics.ListCount - 1
            If .selectedTopics.Selected(x) Then
                For y = 0 To .availableTopics.ListCount - 1
                    If .availableTopics.List(y) = .selectedTopics.List(x) Then
                        uniqueFlag = False
                    End If
                Next y
                
                If uniqueFlag Then
                    .availableTopics.AddItem .selectedTopics.List(x)
                End If
                
                Call AddRemoveAttachments(.selectedTopics.List(x), 2)
                                
                .selectedTopics.RemoveItem (x)
                Exit For
            End If
        Next x
        
        For x = 0 To .selectedTopics.ListCount - 1
            If .selectedTopics.Selected(x) = True Then
                .selectedTopics.Selected(x) = False
            End If
        Next x
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,050
Messages
6,122,868
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top