+ Reply to Thread
Results 1 to 11 of 11

Moving multiple column rows between 2 listboxes

Hybrid View

  1. #1
    Registered User
    Join Date
    08-22-2013
    Location
    Frome England
    MS-Off Ver
    Excel 2013
    Posts
    6

    Moving multiple column rows between 2 listboxes

    Hi

    I have a userform with 2 listboxss. One listbox contains Currency info with 13 columns in it. The first column specifies the currency (GBP, Euro etc) and the other 12 are decimal values for each of the 12 months.

    When the user double clicks a row I need to copy the complete row to the other listbox. It can go from right to left and left to right.

    Im using this code:

    If numCols > 1 Then
    For i = 1 To numCols - 1
    insertedItem = lstMoveTo.ListCount - 1
    lstMoveTo.List(insertedItem, i) = lstMoveFrom.List(itemIndex, i)
    Next i
    End If

    However only the first 10 columns are copied due to constraints on listboxes with unbound data sources.

    Any body have any ingenious ways to get round this?

    Thanks

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645
    First can I ask why you want to move the12 columns?

    What's the next step after you've done that?

    Does it involve choosing a month?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    08-22-2013
    Location
    Frome England
    MS-Off Ver
    Excel 2013
    Posts
    6

    Re: Moving multiple column rows between 2 listboxes

    Hi Norie

    Columns in the left currency listbox are values saved into a sheet in the current xls.

    The listbox on the right are currency values coming in from a loaded xls.

    So i can remove currency entries from the current xls, or import as i wish from the import xls

    Cheers

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Moving multiple column rows between 2 listboxes

    Sorry you've kind of lost me there.

    You have 2 listboxes each populated from a different workbook and you want to move data to and fro between them.

    Is that all you want to do?
    Last edited by Norie; 09-05-2013 at 09:44 AM.

  5. #5
    Registered User
    Join Date
    08-22-2013
    Location
    Frome England
    MS-Off Ver
    Excel 2013
    Posts
    6

    Re: Moving multiple column rows between 2 listboxes

    Yeah thats it, quite simple !!

    apart from the fact you cant move more than 10 columns using ".item = " from one listbox to another !

    Thanks for your help

    Darren

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Moving multiple column rows between 2 listboxes

    Darren

    I know that's a problem and I'm kind of trying to think if there's a way round it by taking a slightly different approach.

    You can actually do it with arrays (or ranges) and List but that's a bit fiddly.

    Any chance you could upload a sample workbook?

  7. #7
    Registered User
    Join Date
    08-22-2013
    Location
    Frome England
    MS-Off Ver
    Excel 2013
    Posts
    6

    Re: Moving multiple column rows between 2 listboxes

    Hi

    Cant send a worksheet as the code is created on the fly in a userform.

    But, heres the code to move the record between listboxes

        'if the list is not empty
        If lstMoveFrom.ListIndex <> -1 Then
    
            itemIndex = lstMoveFrom.ListIndex
            
            With lstMoveFrom
                
                If .Selected(itemIndex) Then
                    lstMoveTo.AddItem .List(itemIndex)
                    'if this is a multicolumn list then add other column info
                    If numCols > 1 Then
                        For i = 1 To numCols - 1
                            insertedItem = lstMoveTo.ListCount - 1
                            lstMoveTo.List(insertedItem, i) = lstMoveFrom.List(itemIndex, i)
                        Next i
                    End If
                    .RemoveItem itemIndex
                End If
            End With
    Thanks

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Moving multiple column rows between 2 listboxes

    Why does that stop you uploading a workbook?

  9. #9
    Registered User
    Join Date
    08-22-2013
    Location
    Frome England
    MS-Off Ver
    Excel 2013
    Posts
    6

    Re: Moving multiple column rows between 2 listboxes

    Cause the real reason is that the xls has loads of confidential data and so does the workbook to import.

    I'm working on storing the MoveTo records and fields into an array, then adding the MoveFrom record and using the MoveTo.list to add them all back.

    If I cant get it working ill create a test xls in the morning and submit that.

    Thanks

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

    Re: Moving multiple column rows between 2 listboxes

    if you populate with array you can exceed 10 columns and use the List array property.

    userform with 2 listboxes and a button to transfer selected list row from 1 to the other.

    Private Sub CommandButton1_Click()
    
        Dim lngIndex As Long
        Dim lngItem As Long
        Dim lngNewIndex As Long
        
        If ListBox2.ListCount = 0 Then
            ' initial populate
            ReDim vntDummy(1 To 1, 1 To ListBox1.ColumnCount)
            ListBox2.ColumnCount = ListBox1.ColumnCount
            ListBox2.List = vntDummy
            lngNewIndex = 0
        Else
            ListBox2.AddItem ""
            lngNewIndex = ListBox2.ListCount - 1
        End If
        
        lngIndex = ListBox1.ListIndex
        For lngItem = 0 To ListBox1.ColumnCount - 1
            ListBox2.List(lngNewIndex, lngItem) = ListBox1.List(lngIndex, lngItem)
        Next
        
    End Sub
    
    Private Sub UserForm_Initialize()
    
        Dim vntData As Variant
        
        vntData = Sheet1.Range("A1:O4")
        ListBox1.ColumnCount = 15
        ListBox1.List = vntData
            
    End Sub
    Attached Files Attached Files
    Cheers
    Andy
    www.andypope.info

  11. #11
    Registered User
    Join Date
    08-22-2013
    Location
    Frome England
    MS-Off Ver
    Excel 2013
    Posts
    6

    Re: Moving multiple column rows between 2 listboxes

    Hi Andy

    I got it working using the array method.

        ' if the list is not empty
        If lstMoveFrom.ListIndex <> -1 Then
    
            itemIndex = lstMoveFrom.ListIndex
            insertedItem = 0
    
            With lstMoveFrom
    
                If .Selected(itemIndex) Then
                
                    'if this is a multicolumn list then add other column info
                    If numCols > 1 Then
    
                        If lstMoveTo.ListCount > 0 Then insertedItem = lstMoveTo.ListCount
                        ReDim dummyArr(insertedItem, numCols - 1)
                
                        ' Get all the rows and columns from the MoveTo list
                        For i = 0 To insertedItem - 1
                            For j = 0 To numCols - 1
                                dummyArr(i, j) = lstMoveTo.List(i, j)
                            Next
                        Next
                        
                        ' When we add this we only need to add the columns
                        lstMoveTo.AddItem .List(itemIndex)
                                            
                        ' Now add the new columns from the old list
                        For i = 0 To numCols - 1
                            dummyArr(insertedItem, i) = lstMoveFrom.List(itemIndex, i)
                        Next i
    
                        ' Now add the complete array (old and new records) back to the lstMoveTo
                        lstMoveTo.List = dummyArr
    
                    End If
                    .RemoveItem itemIndex
                End If
            End With
    
        End If
    Thanks for the link though.

+ 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. [SOLVED] Moving multiple rows into one row (1 record has 4 rows)
    By dogmaxi in forum Excel - New Users/Basics
    Replies: 8
    Last Post: 06-11-2013, 07:01 PM
  2. [SOLVED] Looping through rows in a column and the moving onto the next column
    By MoonWeazel in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-31-2012, 03:52 AM
  3. Replies: 2
    Last Post: 07-09-2012, 01:44 PM
  4. Replies: 5
    Last Post: 02-07-2012, 04:55 PM
  5. Moving groups of rows in one column to another column
    By sglosemeyer5 in forum Excel General
    Replies: 0
    Last Post: 02-01-2012, 02:55 PM

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