Drag item in list box

S

Steven

Is there a way to grap an item in a list box and move it to another position
order in the list box.

Thank you,

Steven
 
J

JLGWhiz

You cannot drag and drop in a ListBox. You would have to RemoveItem and
AddItem. Sample below.

Private Sub ListBox1_Click()
Me.ListBox1.RemoveItem "2"
Me.ListBox1.AddItem "Widget", 2
End Sub
 
J

JLGWhiz

P.S. The code uses Index Numbers to determine which item to remove and add.
A ListBox is zero based so in this case it would actually be the third item
that was removed and replaced.
 
J

JLGWhiz

One other point, the RemoveItem and AddItem will fail if your initial load
method for the ListBox was RowSource or ListFillRange. In those cases you
simply change the range line up.
 
R

Rick Rothstein

Never say never JLGWhiz. Here is a method, different from Harald's (I think
it indicates what has moved more visually than Harald's), that allows you to
drag and drop items within a ListBox (on a UserForm). To move an item, press
the Shift key (think of the Shift key as a memory aid... Shift for shifting
items), either before or after selecting your item , click on the item and
drag the highlight to the position where you want that item to be located
at. The rule for how the items move out of the way is this... if you move
the item to a location earlier in the list than where it currently is, the
item you drop it on (and all items following it) moves down the list to make
room for the item... if you move the item to a location later in the list
than its where it currently is, the item you drop it on (and all preceding
items) moves up the list to make room for it. This movement rule allows you
to move the item to either end without problem (you would not be able to
slot the item at one end or the other if the list always moved in the same
direction). Okay, just paste the following code into the UserForm's code
window (see additional comments after the code)...

'*************** START OF CODE ***************
Dim DragIndex As Long
Dim SecondTime As Boolean
Dim DragActive As Boolean
Dim MouseDownFlag As Boolean

Private Sub ListBox1_Click()
With ListBox1
If SecondTime Then
SecondTime = False
Exit Sub
ElseIf MouseDownFlag Then
DragIndex = .ListIndex
MouseDownFlag = False
DragActive = True
SecondTime = True
End If
End With
End Sub

Private Sub ListBox1_MouseDown(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, ByVal Y As Single)
With ListBox1
If Shift = 1 Then
MouseDownFlag = True
.ListIndex = -1
End If
End With
End Sub

Private Sub ListBox1_MouseUp(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, ByVal Y As Single)
Dim ListText As String
With ListBox1
If DragActive Then
ListText = .List(DragIndex)
.RemoveItem DragIndex
.AddItem ListText, .ListIndex - (.ListIndex > DragIndex)
For X = 0 To .ListCount - 1
If .List(X) = ListText Then
.ListIndex = X
Exit For
End If
Next
End If
DragActive = False
End With
End Sub
'*************** END OF CODE ***************

If you want to test this out on a brand new UserForm, just place a ListBox
on the UserForm and use this event procedure to load up the ListBox with
sample data to drag around (don't forget to press the Shift key while
dragging)...

Private Sub UserForm_Initialize()
Dim X As Long
With ListBox1
For X = 0 To 30
.AddItem "Item #" & CStr(X + 1)
Next
End With
End Sub

Note... I put the ListBox name in With statements to make it easier to
change the referenced list box if you used a ListBox with a different name
than ListBox1. You would have to change the event declaration statements and
the With statements changing my example name of ListBox1 to whatever name
you gave your ListBox.
 
R

Rick Rothstein

I made use of a badly named variable in the MouseUp event code for the
ListBox... I use X as a loop counter, but that X was declared in the event
procedure header. As it turns out, this doesn't affect the functionality of
the code as written, but I would consider it bad practice. What I should
have done is Dim a variable specifically to be used for the loop counter.
Here is how that MouseUp procedure should have been written (I would
recommend substituting the code below for the MouseUp event code I posted
earlier)...

Private Sub ListBox1_MouseUp(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, ByVal Y As Single)
Dim Idx As Long
Dim ListText As String
With ListBox1
If DragActive Then
ListText = .List(DragIndex)
.RemoveItem DragIndex
.AddItem ListText, .ListIndex - (.ListIndex > DragIndex)
For Idx = 0 To .ListCount - 1
If .List(Idx) = ListText Then
.ListIndex = Idx
Exit For
End If
Next
End If
DragActive = False
End With
End Sub
 
Top