+ Reply to Thread
Results 1 to 9 of 9

Transferring data from listbox to listbox

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Transferring data from listbox to listbox

    I have two listboxes (listbox1 and listbox2) with a button between them to move selected data from listbox1 to listbox2. I also have a button to reverse this process, i.e. click on the data in listbox2 and transfer it back to listbox1....great!

    When the data moves back into listbox1 it drops to the bottom of the list.

    Is there any way of getting the data to move back to its original position(s) (where multiple selections are made?

    Thanks in advance,

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,488

    Re: Transferring data from listbox to listbox

    The .AddItem method takes two arguments, the text and position to insert in list. By default (no argument) if is appended to the list
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Re: Transferring data from listbox to listbox

    Ok, I got that so I have assigned their list positions...but how do I code to put them back where they are meant to be?

    It's going to be on a button click event but is it to refresh the originating listbox or code the position directly back in?

    Sorry, I'm a little green....

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,488

    Re: Transferring data from listbox to listbox

    How are you storing the original postition for each item?
    Can you post example workbook of what you have so far

  5. #5
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Re: Transferring data from listbox to listbox

    The positions come from the code relating the the userform. Is there any way of doing it solely through vba?

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,488

    Re: Transferring data from listbox to listbox

    userform with 2 listboxes and 2 commandbuttons.

    Private m_vntList As Variant
    Private m_blnLstA() As Boolean
    Private m_blnLstB() As Boolean
    
    Private Sub CommandButton1_Click()
    
        Dim lngIndex As Long
        Dim lngItem As Long
        
        lngIndex = 0
        For lngItem = 0 To UBound(m_blnLstA)
            If m_blnLstA(lngItem) Then
                If ListBox1.Selected(lngIndex) Then
                    m_blnLstA(lngItem) = False
                    m_blnLstB(lngItem) = True
                End If
                lngIndex = lngIndex + 1
            End If
        Next
        
        ListBox1.Clear
        ListBox2.Clear
        For lngItem = 0 To UBound(m_blnLstA)
            If m_blnLstA(lngItem) Then
                ListBox1.AddItem m_vntList(lngItem)
            End If
            If m_blnLstB(lngItem) Then
                ListBox2.AddItem m_vntList(lngItem)
            End If
        Next
        
    End Sub
    
    Private Sub CommandButton2_Click()
    
        Dim lngIndex As Long
        Dim lngItem As Long
        
        lngIndex = 0
        For lngItem = 0 To UBound(m_blnLstA)
            If m_blnLstB(lngItem) Then
                If ListBox2.Selected(lngIndex) Then
                    m_blnLstB(lngItem) = False
                    m_blnLstA(lngItem) = True
                End If
                lngIndex = lngIndex + 1
            End If
        Next
        
        ListBox1.Clear
        ListBox2.Clear
        For lngItem = 0 To UBound(m_blnLstA)
            If m_blnLstA(lngItem) Then
                ListBox1.AddItem m_vntList(lngItem)
            End If
            If m_blnLstB(lngItem) Then
                ListBox2.AddItem m_vntList(lngItem)
            End If
        Next
    
    End Sub
    
    Private Sub UserForm_Initialize()
    
        Dim lngIndex As Long
        
        m_vntList = Array("A", "B", "C", "D", "E")
        ListBox1.List = m_vntList
        
        ReDim m_blnLstA(0 To ListBox1.ListCount - 1)
        ReDim m_blnLstB(0 To ListBox1.ListCount - 1)
        
        For lngIndex = 0 To ListBox1.ListCount - 1
            m_blnLstA(lngIndex) = True
            m_blnLstB(lngIndex) = False
        Next
        
    End Sub

  7. #7
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Re: Transferring data from listbox to listbox

    All I can say is awesome.....

    Thank you,

  8. #8
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Re: Transferring data from listbox to listbox

    I have found an issue when I adapt this code for the use I am putting it to....

    I make a selection from listbox1 and place it into listbox 2. I then click a button to move the selection in listbox 2, add it to some other information and place it in listbox 3.

    At the moment once I move the selections into listbox 3 and then go to make a further selection from listbox1 my further selection appears in listbox2 along with the first selection I made. I can't figure out why?

    listbox1 and 2 are Multi select enabled.

    So in the above example, if i select "A" on it's own and then move "A" into a listbox 3, then go back into listbox1 and select "B" on it's own, when I click the button to move it to listtbox2 both "A" and "B" appear?

  9. #9
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Re: Transferring data from listbox to listbox

    I have used a workaround now to sort the listbox when an item is added back into it, as follows. At the moment at least it seems to work.

    Dim i As Long
        Dim j As Long
        Dim temp As Variant
         
        With Me.LBOfficerchoice
        For j = LBound(.List) To UBound(.List) - 1
        For i = LBound(.List) To UBound(.List) - 1
        If .List(i) > .List(i + 1) Then
        temp = .List(i)
        .List(i) = .List(i + 1)
        .List(i + 1) = temp
        End If
        Next i
        Next j
        End With

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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