Multiselect listboxes

theduckman16

New Member
Joined
Feb 4, 2011
Messages
16
Hello -

I would greatly appreciate some help with a problem I have come across.

I have a form that has two listboxes and listbox1 is populated with the column headers in my worksheet. I want to be able to have the user select columns on the left and "add" them to listbox2. Here is the code that I have so far:

Code:
Private Sub CommandButton1_Click()
strMsg = "You chose this entry from the list box: " & vbCr
For i = 1 To ListBox1.ListCount
    If ListBox1.Selected(i - 1) = True Then
        strMsg = strMsg & ListBox1.List(i - 1) & vbCr
    End If
Next i
MsgBox strMsg
 
Last edited:
Do you mean you want to transfer the column headers from one listbox to another?

ie move and delete

Try this lightly checked code.
Code:
Option Explicit
 
Private Sub CommandButton1_Click()
Dim I As Long
    For I = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(I) Then
            ListBox2.AddItem ListBox1.List(I)
        End If
        Next I
 
     For I = ListBox1.ListCount - 1 To 0 Step -1
        If ListBox1.Selected(I) Then
            ListBox1.RemoveItem I
        End If
     Next I
 
End Sub
Private Sub UserForm_Initialize()
Dim rng As Range
 
    ' first header on Sheet1
    Set rng = Worksheets("Sheet1").Range("A1")
 
    ' populate ListBox1 with headers from Sheet1
    While rng.Value <> ""
        ListBox1.AddItem rng.Value
 
        Set rng = rng.Offset(, 1)
    Wend
 
    ListBox1.MultiSelect = fmMultiSelectMulti
 
End Sub
I'm pretty sure there's a way to do it without 2 loops, but can't remember right now what it is.:)

I have already figured out how to transfer the list from one listbox to another. Now I need to take the list that I transfered over and delete all of the columns that are not in column 2 in my worksheet.

In the end I would like to loop that through all of the tabs in my workbook.

Let me know if this isn't clear enough.
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

Forum statistics

Threads
1,215,368
Messages
6,124,521
Members
449,169
Latest member
mm424

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