Consulting

Results 1 to 10 of 10

Thread: Solved: Reordering of listbox items.

  1. #1
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location

    Solved: Reordering of listbox items.

    I?ve created a userform to assist with inserting photos into a word document. Part of the process is to create a text file which contains the following information: Order, Path, Name, Description. I would like the facility to reorder the photos by dragging, or otherwise moving the descriptions within a userform listbox or similar
    Sometimes only minor changes are required, other time there may be many. I don?t want to have to select each in turn to drag to another list.
    The final result is to be a reordered text file containing the new order number against each Path/Name/Description.
    Any thoughts?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  2. #2
    MS Excel MVP VBAX Mentor Andy Pope's Avatar
    Joined
    May 2004
    Location
    Essex, England
    Posts
    344
    Location
    I would use a couple of buttons to allow selected items to be moved up/down the list.
    You can set the listbox to allow multiple selections.
    [vba]Private Sub CommandButton2_Click()
    '
    ' Move Items Up, allow multi selected items to move
    '
    Dim lngIndex As Long
    Dim lngStartIndex As Long
    Dim blnSelected() As Boolean

    With ListBox1
    ReDim blnSelected(.ListCount) As Boolean
    For lngIndex = 0 To .ListCount - 1
    blnSelected(lngIndex) = .Selected(lngIndex)
    Next

    lngStartIndex = -1
    For lngIndex = 0 To .ListCount
    If blnSelected(lngIndex) Then
    If lngStartIndex = -1 Then lngStartIndex = lngIndex
    Else
    If lngStartIndex > 0 Then
    SwapListboxItems ListBox1, lngStartIndex - 1, lngIndex - 1
    lngStartIndex = -1
    Else
    lngStartIndex = -1
    End If
    End If
    Next
    End With

    End Sub
    Private Sub CommandButton3_Click()
    '
    ' Move Items Down, allow multi selected items to move
    '
    Dim lngIndex As Long
    Dim lngStartIndex As Long
    Dim blnSelected() As Boolean

    With ListBox1
    ReDim blnSelected(.ListCount) As Boolean
    For lngIndex = 0 To .ListCount - 1
    blnSelected(lngIndex) = .Selected(lngIndex)
    Next

    lngStartIndex = -1
    For lngIndex = 0 To .ListCount - 1
    If blnSelected(lngIndex) Then
    If lngStartIndex = -1 Then lngStartIndex = lngIndex
    Else
    If lngStartIndex >= 0 Then
    SwapListboxItems ListBox1, lngIndex, lngStartIndex
    lngStartIndex = -1
    End If
    End If
    Next
    End With

    End Sub

    Sub SwapListboxItems(Lst As MSForms.ListBox, FromIndex As Long, ToIndex As Long)
    '
    ' Swap listbox items
    '
    ReDim strSubItemText(Lst.ColumnCount - 1) As String
    Dim lngSubItemIndex As Long

    For lngSubItemIndex = 0 To Lst.ColumnCount - 1
    strSubItemText(lngSubItemIndex) = Lst.List(FromIndex, lngSubItemIndex)
    Next

    Lst.RemoveItem FromIndex
    Lst.AddItem strSubItemText(0), ToIndex

    For lngSubItemIndex = 1 To Lst.ColumnCount - 1
    Lst.List(ToIndex, lngSubItemIndex) = strSubItemText(lngSubItemIndex)
    Next

    End Sub[/vba]

    Using drag and drop is possible but gets very messy very quickly when done within the same listbox. Also scrolling whilst dragging is not automatic so trying to drag the top item to the bottom on a list that requires scrolling doesn't work.
    Cheers
    Andy

  3. #3
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Thanks Andy,
    Just what I was looking for. To simplify presentation, I renamed your button codes and added a SpinButton as follows
    Regards
    Malcolm
    [VBA]
    Private Sub SpinButton1_Change()
    If SpinButton1 = 1 Then MoveUp
    If SpinButton1 = -1 Then MoveDown
    SpinButton1.Value = 0
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Final version.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    I'm probably missing something Malcolm...please let me know if I misunderstand but it doesn't save the changes to the order to the text file for me unless I comment out this line:
    BubbleSort Data
    in cmdReWrite_Click

    This is a good idea though..I can see where it would be useful
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Steve,
    There is no "save" procedure. The process is to:
    Change the order number stored in the first three characters of each line listed in the array
    Sort the array using BubbleSort
    Kill the old file
    Create a new file base on the reordered array.

    Let me know if you find any problem.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    I see that but its supposed to create the new file with the same path and filename as the opened file..? I just expected the changes in order to be reflected in the textfile after the operation.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    The textfile is closed after each step of reading/writing (a.close), When ReWrite is run, the text file is rewritten then reopened to populate the listbox.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    I follow it now....thanks Malcolm
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  10. #10
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Heya Strangers!

    Quote Originally Posted by Andy Pope View Post
    I would use a couple of buttons to allow selected items to be moved up/down the list.
    Just wanted to pop a quick note to say how awesome this code is, Andy. I needed to do the same thing here, copied, pasted, updated my form names and it just worked. Beautiful!

    Hope you guys are all well these days.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





Posting Permissions

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