ListBox MultiSelect - moving selected items up the list

RawlinsCross

Active Member
Joined
Sep 9, 2016
Messages
437
I have a listbox on a userform with several items in in. I'd like to be able to move them in groups up the list upon clicking a command button on the userform. Has anyone been through this challenge?
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Where exactly do you want to move them?

Right to the top? Up one row?

Will the 'groups' be contiguous?
 
Upvote 0
Good points Norie - I was thinking about the 'contiguous' angle after posting. But yes, they would be contiguous and reordered up one row as a block.
 
Upvote 0
Give this a try.
VBA Code:
Option Explicit

Private Sub cmdUp_Click()
Dim arrItems As Variant
Dim cnt As Long
Dim idx As Long
Dim idxTop As Long

    With ListBox1
        ReDim arrIndexItems(1 To .ListCount)
        ReDim arrItems(1 To .ListCount)
        For idx = .ListCount - 1 To 0 Step -1
            If .Selected(idx) Then
                idxTop = idx
                cnt = cnt + 1
                arrItems(cnt) = .List(idx, 0)
               .RemoveItem idx
            End If
        Next idx
    End With
    
    If idxTop <> 0 Then
        ReDim Preserve arrItems(1 To cnt)
        
        
        For idx = 1 To cnt
            With ListBox1
                .AddItem arrItems(idx), idxTop - 1
            End With
        Next idx
    End If
    
End Sub

Private Sub UserForm_Initialize()
Dim arr As Variant

    arr = Evaluate("""Item""&ROW(1:20)")
    With ListBox1
        .List = arr
        .MultiSelect = fmMultiSelectExtended
    End With
    
End Sub
 
Upvote 0
Solution
Very good start - just dissecting the code now. Now if I wanted to maintain the highlighting in the event I want to keep clicking "UP" and by doing so keep moving the block up?
 
Upvote 0
To keep the items selected you could try this, untested, adjustment to the code.
VBA Code:
        For idx = 1 To cnt
            With ListBox1
                .AddItem arrItems(idx), idxTop - 1
                .Selected(idxTop-1) = True
            End With
        Next idx
 
Upvote 0
Thanks, that did that trick! I also added at the top of the routine:

VBA Code:
If .Selected(0) = True Then Exit Sub

If not, any contiguous selection that includes the first row will be deleted permanently.
 
Upvote 0

Forum statistics

Threads
1,214,596
Messages
6,120,438
Members
448,966
Latest member
DannyC96

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