VBA: how to adapt this listbox code when multiselect is enabled?

Kimberley

New Member
Joined
Nov 27, 2011
Messages
22
Hi,

I would like to adapt 2 codes that are working perfectly with a listbox with multiselect disabled. Here are the 2 codes written respectively by MickG and vaskov17 (thanks again):

Code:
Private Sub deplacer_haut_Click()

'Move up
Dim temp As String
Dim Ray
Dim ac As Integer
Dim n
With ListBox1
    Ray = .List
    n = .ListIndex
If Not .ListIndex = 0 And Not .Value = vbNullString Then
    For ac = 1 To 2
        temp = .List(.ListIndex, ac)
        Ray(.ListIndex, ac) = Ray(.ListIndex - 1, ac)
        Ray(.ListIndex - 1, ac) = temp
    Next ac
        .List = Ray
        .ListIndex = n - 1
End If
End With

End Sub

Code:
Private Sub Effacer_Click()
    Dim i As Integer
    
    With ListBox1
    
    If Not .ListIndex = -1 And Not .Value = vbNullString Then
        .RemoveItem (.ListIndex)
        
        If .ListIndex <> .ListCount - 1 Then
            For i = .ListIndex To .ListCount - 1
                .List(i, 0) = .List(i, 0) - 1
            Next i
        End If
    End If
    End With
End Sub

You can download the .xlsm file there:
UserForm - Multiselect.xlsm - 0.03MB

Thanks in advance for the help!
 

Some videos you may like

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,218
Office Version
  1. 365
Platform
  1. Windows
What exactly does the original code do and what is it you want to change?

I think the first one is moving items in a listbox and the 2nd is deleting items.
 

Kimberley

New Member
Joined
Nov 27, 2011
Messages
22

ADVERTISEMENT

You got it right, it's better to download the excel file so you can so how those 2 codes fit in the userform. I would like to adapt this perfectly working code for a listbox with multiselect enabled.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,218
Office Version
  1. 365
Platform
  1. Windows
And how would I be downloading these files?:)

PS It would help if you explained exactly what the code does, eg what item(s) are being moved/removed? Is it random? Is it based on some criteria?
 

Kimberley

New Member
Joined
Nov 27, 2011
Messages
22
I put a link in the first post to download the file ;)

That would be to (re)organize data from the listbox. This code would be used to be able to move up/down/delete a group of selected items in the listbox after pressing the corresponding button (up/down/delete). The code provided is working perfectly. It doesn't generate errors at all even when I try to move up the 1st item, or move down the last one. But it's working for only one item selected at a time (with the multiselect option disable in the listbox options).

It will be easier to understand after seeing the excel file. Let me know if the link is not working.
Thanks!
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,218
Office Version
  1. 365
Platform
  1. Windows
I tried that and it doesn't seem to work.

Unless I've missed something, there's a lot of pop-ups etc.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,670
Messages
5,597,483
Members
414,146
Latest member
marginmakerb

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
Top