Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 389
Default Drag item in list box

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default Drag item in list box

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


"Steven" wrote:

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

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default Drag item in list box

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.

"Steven" wrote:

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

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default Drag item in list box

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.

"Steven" wrote:

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

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 449
Default Drag item in list box

Hi Steven

Sure it is:
http://www.dailydoseofexcel.com/arch...drag-and-drop/

Best wishe Harald

"Steven" wrote in message
...
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




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Drag item in list box

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.

--
Rick (MVP - Excel)


"Steven" wrote in message
...
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


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Drag item in list box

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

--
Rick (MVP - Excel)


"Rick Rothstein" wrote in message
...
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.

--
Rick (MVP - Excel)


"Steven" wrote in message
...
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



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Select a list in 1 cell by selecting an item from another list in Jonners Excel Discussion (Misc queries) 2 July 10th 09 10:31 PM
List and subtotal selected items, then print separate item list TitanG Excel Worksheet Functions 0 September 8th 08 09:07 PM
I need to pair each item on one list to each item on another list Peter R. Excel Worksheet Functions 1 August 24th 07 03:04 AM
Dropdown List - list item endings not visible if column too narrow AK9955 Excel Discussion (Misc queries) 2 April 27th 07 09:02 AM
Selecting an Item from a List and getting a different item to pop. Matt Excel Worksheet Functions 1 December 7th 04 02:37 PM


All times are GMT +1. The time now is 08:11 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"