move items in multi column listbox

  • Thread starter Thread starter christobal
  • Start date Start date
C

christobal

Have A listbox on a userform which has been populated from the entry o
data from 4 dropdown lists. After population is completed the listbo
contains 4 columns with "x" rows. Have added a command which allows th
user to move the selected row up in the list. The problem with th
following code is that the first column entry is moved without the dat
of the corresponding 3 columns
can any one help me on this one
------------------------------------------

Private Sub cmdup_Click()
' stores the two items


Dim SelectedData As String
Dim AboveData As String
' Stores the index of the selected item
Dim RowNumber As Integer
RowNumber = ListBox1.ListIndex

If RowNumber = 0 Then
ListBox1.SetFocus
Exit Sub
Else
SelectedData = ListBox1.List(RowNumber)
AboveData = ListBox1.List(RowNumber - 1)

'move selected data down 1 position
'(meanwhile the other data item takes its place)
ListBox1.List(RowNumber) = AboveData
ListBox1.List(RowNumber - 1) = SelectedData

' select the list item you just moved
ListBox1.Selected(RowNumber - 1) = True
ListBox1.SetFocus
End If
End Sub
 
Found the solution with this following snippet

Private Sub cmdMoveUp_Click()
'move the selected item up one row

Dim i As Integer, j As Integer
Dim s As String

With ListBox1
i = .ListIndex
If i > 0 Then
'only do this if an item has been selected
'and if the item isn't at the top already
' j = column index where 4 columns = 0 to 3
For j = 0 To 3
s = .List(i, j)
.List(i, j) = .List(i - 1, j)
.List(i - 1, j) = s
Next j
.ListIndex = i - 1
End If
End With

End Su
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top