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
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