+ Reply to Thread
Results 1 to 5 of 5

Help with Add/Remove Items from One List Box to Another

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-30-2016
    Location
    Gillette, WY
    MS-Off Ver
    Office 365
    Posts
    230

    Help with Add/Remove Items from One List Box to Another

    Hello all,

    I am having trouble with some code to populate one User Form list box based on the values in another. The user has a list of available colors on the left and uses command buttons to either

    1) Add All
    2) Add Selected
    3) Remove Selected
    4) Remove All

    The Add All and Remove All functions are working but the Add/Removed selected is not.

    My code for Add Selected:
    Private Sub CmdBtn_AddColor_Click()
    '
    'Source: Move user selected values from list box 1 to list box 2: https://analysistabs.com/excel-vba/listbox-adding-clearing-items-multiple-selection/
    '
    
        Dim iCnt As Integer
        
        For iCnt = 0 To Me.LstBx_ColorsSelected.ListCount - 1
        If Me.LstBx_ColorsSelected.Selected(iCnt) = True Then
            Me.LstBx_ColorsAvailable.AddItem Me.LstBx_ColorsSelected.List(iCnt)
            End If
        Next
        For iCnt = Me.LstBx_ColorsSelected.ListCount - 1 To 0 Step -1
        If Me.LstBx_ColorsSelected.Selected(iCnt) = True Then
            Me.LstBx_ColorsSelected.RemoveItem iCnt
            End If
        Next
            
    End Sub
    My code for Remove Selected, this function simply needs to remove the selected items from the list box, it does not need to repopulate them back to the other list box.

    Private Sub CmdBtn_RemoveColor_Click()
    '
    'Source: Move user selected values from list box 1 to list box 2: https://analysistabs.com/excel-vba/listbox-adding-clearing-items-multiple-selection/
    '
    
        Dim iCnt As Integer
        
        For iCnt = 0 To Me.LstBx_ColorsSelected.ListCount - 1
        If Me.LstBx_ColorsSelected.Selected(iCnt) = True Then
            Me.LstBx_ColorsSelected.RemoveItem
        End If
            
        
    End Sub
    A copy of my project is attached. I have not yet written code for the other functions of the User Form.
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Help with Add/Remove Items from One List Box to Another

    Private Sub CmdBtn_AddColor_Click()
    '
    'Source: Move user selected values from list box 1 to list box 2: https://analysistabs.com/excel-vba/listbox-adding-clearing-items-multiple-selection/
    '
    
        Dim iCnt As Integer
        
        For iCnt = 0 To Me.LstBx_ColorsAvailable.ListCount - 1
        If Me.LstBx_ColorsAvailable.Selected(iCnt) = True Then
            Me.LstBx_ColorsSelected.AddItem Me.LstBx_ColorsAvailable.List(iCnt)
            End If
        Next
            
    End Sub
    Private Sub CmdBtn_RemoveColor_Click()
    '
    'Source: Move user selected values from list box 1 to list box 2: https://analysistabs.com/excel-vba/listbox-adding-clearing-items-multiple-selection/
    '
    
        Dim iCnt As Integer
        
        For iCnt = Me.LstBx_ColorsSelected.ListCount - 1 To 0 Step -1
            If Me.LstBx_ColorsSelected.Selected(iCnt) = True Then
                Me.LstBx_ColorsSelected.RemoveItem iCnt
            End If
        Next
            
        
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Contributor
    Join Date
    03-30-2016
    Location
    Gillette, WY
    MS-Off Ver
    Office 365
    Posts
    230

    Re: Help with Add/Remove Items from One List Box to Another

    Worked perfectly. Thank you. What was wrong with my initial code. Very new to VBA, your code looks similar but if you could point out the error, I would appreciate it. Thank you again!

  4. #4
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,315

    Re: Help with Add/Remove Items from One List Box to Another

    And you could shorten code a bit in other places, e.g.:
    Option Explicit
    
    Private rngColorList As Variant
    
    Private Sub UserForm_Initialize()
    Dim last_row As Long
    
        With Sheets("Colors Available")
            last_row = .Cells(.Rows.Count, "A").End(xlUp).Row
            rngColorList = .Range("A2:A" & last_row).Value
        End With
        
        Me.LstBx_ColorsAvailable.List() = rngColorList
        
    'Dim rngColorList As Range
    'Dim shtColorSource As Worksheet
    '
    '    'Pull dynamic range to populate LstBx_ColorsAvailable
    '    Set shtColorSource = Worksheets("Colors Available")
    '
    '    For Each rngColorList In shtColorSource.Range("lst_ColorsAvailable")
    '        Me.LstBx_ColorsAvailable.AddItem rngColorList.Value
    '    Next rngColorList
    End Sub
    
    Private Sub CmdBtn_Close_Click()
        Me.Hide
        Unload Me
    End Sub
    
    Private Sub CmdBtn_AddAllColors_Click()
        Me.LstBx_ColorsSelected.List() = rngColorList
        
    'Dim iCnt As Integer
    '
    '    For iCnt = 0 To Me.LstBx_ColorsAvailable.ListCount - 1
    '        Me.LstBx_ColorsSelected.AddItem Me.LstBx_ColorsAvailable.List(iCnt)
    '    Next iCnt
    End Sub

  5. #5
    Forum Contributor
    Join Date
    03-30-2016
    Location
    Gillette, WY
    MS-Off Ver
    Office 365
    Posts
    230

    Re: Help with Add/Remove Items from One List Box to Another

    Thank you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Remove items from dropdown list when item value = 0
    By technolog in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-21-2017, 09:57 AM
  2. Best way to remove items from list
    By TheRobsterUK in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-12-2014, 12:32 PM
  3. Replies: 1
    Last Post: 09-14-2013, 04:55 AM
  4. Replies: 2
    Last Post: 08-23-2011, 08:55 PM
  5. Remove matching items from a workbook based on a master list
    By Apel in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-16-2009, 08:20 PM
  6. cleaning up my list - remove items in the list
    By tmssj2000 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-06-2009, 02:00 AM
  7. remove items from list box (sorry it`s a bit long)
    By bahadirakcan in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-10-2006, 08:11 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1