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:

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Something like this perhaps?

Code:
Private Sub CommandButton1_Click()
' check if anything is selected
If Me.ListBox1.ListIndex = -1 Then Exit Sub
 
For i = 1 To Me.ListBox1.listCount
  If Me.ListBox1.Selected(i - 1) Then
    Me.ListBox2.AddItem Me.ListBox1.List(i - 1)
  Next
Next i
End Sub
 
Upvote 0
Something like this perhaps?

Code:
Private Sub CommandButton1_Click()
' check if anything is selected
If Me.ListBox1.ListIndex = -1 Then Exit Sub
 
For i = 1 To Me.ListBox1.listCount
  If Me.ListBox1.Selected(i - 1) Then
    Me.ListBox2.AddItem Me.ListBox1.List(i - 1)
  Next
Next i
End Sub

That works perfectly. Now do you know a way to delete all of the columns that are not selected in listbox2?

thx
 
Upvote 0
Here is my code thus far.

Code:
Private Sub UserForm_Initialize()
For i = 1 To ActiveSheet.Columns.Count
     Me.ListBox1.AddItem ActiveSheet.Cells(1, i)
Next i
End Sub
 
Private Sub CommandButton1_Click()
Dim i As Integer
If ListBox1.ListIndex = -1 Then Exit Sub
For i = ListBox1.ListCount - 1 To 0 Step -1
    If ListBox1.Selected(i) = True Then
        ListBox2.AddItem ListBox1.List(i)
        ListBox1.RemoveItem (i)
    End If
Next i
End Sub
 
Private Sub cmdRemoveColumns_Click()
Dim i As Integer
If ListBox2.ListIndex = -1 Then Exit Sub
For i = ListBox2.ListCount - 1 To 0 Step -1
    If ListBox2.Selected(i) = True Then
        ListBox1.AddItem ListBox2.List(i)
        ListBox2.RemoveItem (i)
    End If
Next i
End Sub
 
Private Sub cmdOK_Click()
End Sub

I need to be able to delete all of the columns that are not in listbox2.
 
Upvote 0
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.:)
 
Upvote 0
This seems to work, but it also seems a bit convoluted.:)
Code:
Dim I As Long
Dim lCnt As Long
    lCnt = ListBox1.ListCount
    
    Do
    
        If ListBox1.Selected(I) Then
            ListBox2.AddItem ListBox1.List(I)
            ListBox1.RemoveItem I
            lCnt = lCnt - 1
           I = I - 1
        End If
    I = I + 1
    
    Loop Until I >= lCnt
 
Upvote 0
This board has dozens of active threads, staffed by unpaid volunteers. You haven't stumped us, you just haven't waited long enough for a reply.

Are you saying you want to delete the columns from the worksheet that aren't represented in ListBox2?
 
Upvote 0
This board has dozens of active threads, staffed by unpaid volunteers. You haven't stumped us, you just haven't waited long enough for a reply.

Are you saying you want to delete the columns from the worksheet that aren't represented in ListBox2?

Yes, that is correct.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,695
Members
448,979
Latest member
DET4492

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