Moving Items Up and Down in List Box

Ejimenez

New Member
Joined
Nov 10, 2012
Messages
14
I am trying to move items up and down my listbox2 I have the code below however there seems to be an error with the moving down button


Option Explicit
'Move ListBox Items code

Private Sub BTN_moveselectedLeft_Click()
Dim iCtr As Long
For iCtr = 0 To Me.ListBox2.ListCount - 1
Me.ListBox1.AddItem Me.ListBox2.List(iCtr)
Next iCtr
Me.ListBox2.Clear
End Sub
Private Sub BTN_moveselectedRight_Click()
Dim iCtr As Long
For iCtr = 0 To Me.ListBox1.ListCount - 1
Me.ListBox2.AddItem Me.ListBox1.List(iCtr)
Next iCtr
Me.ListBox1.Clear
End Sub

Private Sub BTN_MoveSelecteddown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim i As Integer
'Make sure our item is not the last one on the list.
If ListBox2.SelectedIndex < ListBox2.Items.Count - 1 Then
'Insert places items above the index you supply, since we want
'to move it down the list we have to do + 2
dim i = ListBox2.SelectedIndex + 2
ListBox2.Items.Insert(I, ListBox2.SelectedItem)
ListBox2.Items.RemoveAt (ListBox2.SelectedIndex)
ListBox2.SelectedIndex = i - 1
End If
End Sub

End Sub
Private Sub Worksheet_Activate()
Dim myCell As Range
Dim rngItems As Range
Set rngItems = Sheets("Vendors").Range("B2:B62")
Me.ListBox1.Clear
Me.ListBox2.Clear

With Me.ListBox1
.LinkedCell = ""
.ListFillRange = ""
For Each myCell In rngItems.Cells
If Trim(myCell) <> "" Then
.AddItem myCell.Value
End If
Next myCell
End With
Me.ListBox1.MultiSelect = fmMultiSelectMulti
Me.ListBox2.MultiSelect = fmMultiSelectMulti
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
See if this example is useful:

Code:
' this code goes at the UserForm module
Private Sub MoveUpButton_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    MoveUpButton_Click
End Sub


Private Sub MoveDownButton_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    MoveDownButton_Click
End Sub


Private Sub MoveUpButton_Click()
    
    Exchange 0, -1
        
End Sub


Private Sub MoveDownButton_Click()


    Exchange ListBox1.ListCount - 1, 1


End Sub


Sub Exchange(crit%, os%)
    Dim NumItems%, i%, ItemNum%, TempItem$, TempList(), LB As Object
    
    Set LB = Me.ListBox1
    If LB.ListIndex = crit Then Exit Sub
    NumItems = LB.ListCount
    ReDim TempList(0 To NumItems - 1)
    For i = 0 To NumItems - 1
        TempList(i) = LB.List(i)
    Next i
    ItemNum = LB.ListIndex
    TempItem = TempList(ItemNum)
    TempList(ItemNum) = TempList(ItemNum + os)
    TempList(ItemNum + os) = TempItem
    LB.List = TempList
    LB.ListIndex = ItemNum + os
End Sub


Private Sub OKButton_Click()
    Unload Me
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,930
Members
452,367
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top