Move the selected item up one position in the list Userform ListBox

giri u

New Member
Joined
Jun 26, 2022
Messages
7
Office Version
  1. 2010
Platform
  1. Windows
i have 4 Column list box i tryed to Move the selected item up one position but only first Column worked.Please Help Any One

Sub MoveUp()
'PURPOSE: Move the selected item up one position in the list

Dim x As Long
Dim Count As Long
Dim Position As Long

'Is there an item selected?
If ListBox1.ListIndex = -1 Then Exit Sub

'Which Item is selected?
For x = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(x) = True Then
Position = x
Count = Count + 1
If Count > 1 Then Exit Sub 'More than 1 item selected
End If
Next x

'Selected item already at the top?
If Position = 0 Then Exit Sub

'Add an item above the current selection
ListBox1.AddItem ListBox1.List(Position), Position - 1

'Remove Original Selection
ListBox1.RemoveItem Position + 1

'Re-select the item that got moved
ListBox1.Selected(Position - 1) = True

End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Try...

VBA Code:
'Add an item above the current selection
ListBox1.AddItem ListBox1.List(Position), Position - 1
ListBox1.List(Position - 1, 1) = ListBox1.List(Position + 1, 1)
ListBox1.List(Position - 1, 2) = ListBox1.List(Position + 1, 2)
ListBox1.List(Position - 1, 3) = ListBox1.List(Position + 1, 3)

However, here's another way...

VBA Code:
Private Sub MoveUp()

    Dim tempList() As Variant
    Dim tempItem As String
    Dim itemIndex As Long
    Dim columnIndex As Long

    If Me.ListBox1.ListIndex <= 0 Then Exit Sub
    
    tempList() = Me.ListBox1.List
    
    itemIndex = Me.ListBox1.ListIndex
    
    For columnIndex = LBound(tempList, 2) To UBound(tempList, 2)
        tempItem = tempList(itemIndex, columnIndex)
        tempList(itemIndex, columnIndex) = tempList(itemIndex - 1, columnIndex)
        tempList(itemIndex - 1, columnIndex) = tempItem
    Next columnIndex
    
    Me.ListBox1.List = tempList
    
End Sub

Hope this helps!
 
Upvote 0
AddItem only adds the data to the first column. To move a multi-column line, you add the item, then also copy the data from the other columns.

I have not taken the time to set up a new file to test this.
Rich (BB code):
Dim c As Long

'Add an item above the current selection
ListBox1.AddItem ListBox1.List(Position), Position - 1
For c = 1 To Me.ListBox1.ColumnCount - 2
   ListBox1.List(Position - 1, c) = ListBox1.List(Position, c)
Next c
 
Upvote 0
Try this:

VBA Code:
Sub MoveUp()
  Dim i As Long, n As Long
  Dim tmp As String
  
  With ListBox1
    n = .ListIndex
    If n > 0 Then
      For i = 0 To 3
        tmp = .List(n - 1, i)
        .List(n - 1, i) = .List(n, i)
        .List(n, i) = tmp
      Next
      .Selected(n - 1) = True
    End If
  End With
End Sub
 
Upvote 0
Try...

VBA Code:
'Add an item above the current selection
ListBox1.AddItem ListBox1.List(Position), Position - 1
ListBox1.List(Position - 1, 1) = ListBox1.List(Position + 1, 1)
ListBox1.List(Position - 1, 2) = ListBox1.List(Position + 1, 2)
ListBox1.List(Position - 1, 3) = ListBox1.List(Position + 1, 3)

However, here's another way...

VBA Code:
Private Sub MoveUp()

    Dim tempList() As Variant
    Dim tempItem As String
    Dim itemIndex As Long
    Dim columnIndex As Long

    If Me.ListBox1.ListIndex <= 0 Then Exit Sub
   
    tempList() = Me.ListBox1.List
   
    itemIndex = Me.ListBox1.ListIndex
   
    For columnIndex = LBound(tempList, 2) To UBound(tempList, 2)
        tempItem = tempList(itemIndex, columnIndex)
        tempList(itemIndex, columnIndex) = tempList(itemIndex - 1, columnIndex)
        tempList(itemIndex - 1, columnIndex) = tempItem
    Next columnIndex
   
    Me.ListBox1.List = tempList
   
End Sub

Hope this helps!
Thanks Sir.
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,774
Members
452,942
Latest member
VijayNewtoExcel

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