Move from one listbox to another.

ccordner

Active Member
Joined
Apr 28, 2010
Messages
355
Good afternoon

I have a subroutine, which is intended to transfer a name from one listbox to another:

VBA Code:
'Initialise End Of List flag
booEndOfList = False
lngLoopCount = 0

With lstLocations

    While booEndOfList = False
        
        'Check if we have reached the end of the list
        If lngLoopCount = .ListCount Then
        
            booEndOfList = True
        
        Else
        
            'Check if it is selected
            If .Selected(lngLoopCount) = True Then
            
                'Capture the location name
                strLocName = .List(lngLoopCount)
            
                'Transfer across and remove from the original list
                lngListPos = insertItem(lstMainLocs, strLocName)
                .RemoveItem (lngLoopCount)
            
                'Mark as selected
                With colLocations.Item(strLocName)
                    .locSelected = True
                End With
            
            Else
            
                'If not selected, move to next item in list
                lngLoopCount = lngLoopCount + 1
            
            End If
        
        End If
    
    Wend

End With

For some reason, this works fine but every now and then it stalls, at the .removeitem line, because it's trying to remove item 1 in a listbox containing only one item. I can't work out what I've misssed.

Any ideas?

Thanks
Chris
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Could try writing a check on ListCount. If Listcount = 1 then do a listbox clear rather than delete
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,626
Members
449,093
Latest member
catterz66

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