Jump to content
News Ticker
  • Welcome to the Community
  • Use the forum to troubleshoot your Excel & VBA Problems
  • We will be launching a new website soon! www.ExcelWTF.com/Learn ! Join us to learn VBA from the ground up!
  • 0
Sign in to follow this  
ZiyaSepp

Populate Listbox from another Listbox

Question

Hi Caleeco, 

 

Follow up question to the one I posted here:

 

 

With Listbox1 I have MultiSelect property set to '1 - frmMultiSelectMulti' so I can choose more than 1 option. How can i get the selection to move to a second list box? 

 

I've tried setting up a button to do this called 'btnTransfer'.

 

HELP!

Z

Share this post


Link to post
Share on other sites

4 answers to this question

Recommended Posts

  • 0

haha I enjoy problem solving with VBA so keep them coming :D

 

Yes we could do that, but it would need a slight change. The original code looks at the list items top to bottom... but if we start removing items, the counter in our loop will cause the code to break :| Luckily we can just start at the end of the list and step backwards :)

 

Private Sub btnTransfer_Click()
'//Source: www.ExcelWTF.com
'//Purpose: Move Selection from List1 to List2. Deletes item from List1
    Dim i As Long
        For i = (ListBox1.ListCount - 1) To 0 Step -1
            If ListBox1.Selected(i) Then
                ListBox2.AddItem ListBox1.List(i, 0), 0
                ListBox1.RemoveItem i
            End If
        Next
End Sub

Hope that helps

Caleeco

  • Like 1

Share this post


Link to post
Share on other sites
  • 0

Hmm, not done that before... but had a crack at it! Assumed your second Listbox is called 'Listbox2'

 

Private Sub btnTransfer_Click()
'//Source: www.ExcelWTF.com
'//Purpose: Move Selection from List1 to List2. Clears selection each time.
    Dim i As Long
    ListBox2.Clear 'Delete this line if you wish to transfer multiple selections
    For i = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(i) Then
            ListBox2.AddItem ListBox1.List(i, 0)
        End If
    Next
End Sub

Please try the code above. Also please note the Listbox2.Clear, this will reset the contents each time the button is clicked. Delete that line if that isnt the intended use case :)

 

Hope that helps

Caleeco

  • Like 1

Share this post


Link to post
Share on other sites
  • 0

Thanks for the super quick replies! That code is almost what I need. Basically, when something moves from Listbox1 to Listbox2, can it also be removed from the original list?

 

Z

Share this post


Link to post
Share on other sites
  • 0

Thanks Caleeco! Works great!

 

I have updated my form layout and want the option to move items back and forth between the two list boxes O.o However, I'm going to give this a go and see if I can get it working myself. 

 

Thanks for your help as always

Z

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×