OzGrid

Move ListBox Items Up & Down

< Back to Search results

 Category: [Excel]  Demo Available 

Move ListBox Items Up & Down

 

Got any Excel/VBA Questions? Free Excel Help

 

Change Order Of List Box Values

Download Example

Add 2 CommandButtons to your UserForm and Name them MoveUp and MoveDown. Then, use the RowSource Property of the Multi Column ListBox to fill the ListBox with a Named Range. Ensure this named range has headings BUT are no included in the range name definition. Set the ColumnHeads Property of the ListBox to True and the ColumnCount to as many columns as the named range has Columns. Set the ColumnWidths as needed.

Private Sub MoveDown_Click() 

    Dim lCurrentListIndex As Long 

    Dim strRowSource As String 

    Dim strAddress As String 

    Dim strSheetName As String 

     

     

    With ListBox1 

        If .ListIndex < 0 Or .ListIndex = .ListCount - 1 Then Exit Sub 

        lCurrentListIndex = .ListIndex + 1 

        strRowSource = .RowSource 

        strAddress = Range(strRowSource).Address 

        strSheetName = Range(strRowSource).Parent.Name 

        .RowSource = vbNullString 

        With Range(strRowSource) 

            .Rows(lCurrentListIndex).Cut 

            .Rows(lCurrentListIndex + 2).Insert Shift:=xlDown 

        End With 

         Sheets(strSheetName).Range(strAddress).Name = strRowSource 

        .RowSource = strRowSource 

        .Selected(lCurrentListIndex) = True 

    End With 

     

End Sub 

 

Private Sub MoveUp_Click() 

    Dim lCurrentListIndex As Long 

    Dim strRowSource As String 

    Dim strAddress As String 

    Dim strSheetName As String 

     

     

    With ListBox1 

        If .ListIndex < 1 Then Exit Sub 

        lCurrentListIndex = .ListIndex + 1 

        strRowSource = .RowSource 

        strAddress = Range(strRowSource).Address 

        strSheetName = Range(strRowSource).Parent.Name 

        .RowSource = vbNullString 

        With Range(strRowSource) 

            .Rows(lCurrentListIndex).Cut 

            .Rows(lCurrentListIndex - 1).Insert Shift:=xlDown 

        End With 

        Sheets(strSheetName).Range(strAddress).Name = strRowSource 

        .RowSource = strRowSource 

        .Selected(lCurrentListIndex - 2) = True 

    End With 

     

End Sub

Download Example

See also:

Inputbox in Excel VBA
Is Workbook Open/Workbook Exists/Worksheet Exists/Auto Filter/How Many Pages Printed
Excel VBA: Create Worksheets for Each Item in an Excel Table of Data
Return Last Chosen Day of Given Month
Find & Return The Last Used Cell On An Excel Worksheet Or Column
Limit Scroll Area on an Excel Worksheet
Filter ListBox Data to Meet a Specified Criteria

 

See also Index to Excel VBA Code and Index to Excel Freebies and Lesson 1 - Excel Fundamentals and Index to how to… providing a range of solutions.

 

Click here to visit our Free 24/7 Excel/VBA Help Forum where there are thousands of posts you can get information from, or you can join the Forum and post your own questions.


Gallery



stars (0 Reviews)