Listbox 1 vers Listbbox2

Bonjour ,

J'ai une base de donnée sous Excel qui contient plusieurs onglets , donc j'ai pensé à construire un UF qui sert à choisir les onglets à exporter dans un nouveau fichier.

Dans mon UF , J'ai deux listbox ( listbox1 = choixonglets & listbox = ongletAexporté , et j'ai deux boutons :

- Ajouter pour faire mon choix.

- Supprimer pour supprimer l'item dans la Listbox2 et l'afficher dans sa première place dans la Listbox1.

==> Le problème : l'item à supprimer dans la list , il ne retourne pas à sa première place , il s'affiche de fin de la list dans Listbox1.

Private Sub Ajouter_Click()
'transfert de la Liste des onglets -> Liste des onglets conservées

Dim g, gmax, n, m, h As Variant
gmax = ListBox1.ListCount - 1

For g = 0 To gmax

If ListBox1.Selected(g) = True Then
ListBox2.AddItem ListBox1.List(g, 0)
m = ListBox2.ListCount - 1
ListBox1.RemoveItem (g)
g = g - 1
End If

'pb de sortie de boucle for en fin de liste
gmax = ListBox1.ListCount - 1
If g = gmax Then Exit For
Next g
End Sub

------------------------------------------------

Private Sub Supprimer_Click()
'transfert de la Liste des onglets conservées -> Liste des onglets
Dim g, gmax, n, m As Variant
gmax = ListBox2.ListCount

For g = 0 To gmax
If ListBox2.Selected(g) = True Then
ListBox1.AddItem ListBox2.List(g, 0)
ListBox2.RemoveItem (g)
g = g - 1
End If
'pb de sortie de boucle for en fin de liste
gmax = ListBox2.ListCount
If g = gmax - 1 Then Exit For
Next g
End Sub

Bonjour Ahmedhamed et bienvenu, bonjour le forum,

Essaie comme ça :

Private Sub Ajouter_Click()
Dim g, gmax, n, m, h As Variant

gmax = ListBox1.ListCount - 1
For g = gmax To 0 Step -1
    If ListBox1.Selected(g) = True Then
        ListBox2.AddItem ListBox1.List(g, 0), 0
        m = ListBox2.ListCount - 1
        ListBox1.RemoveItem (g)
    End If
Next g
End Sub

Private Sub Supprimer_Click()
Dim g, gmax, n, m As Variant

gmax = ListBox2.ListCount
For g = gmax To 0 Step -1
    If ListBox2.Selected(g) = True Then
        ListBox1.AddItem ListBox2.List(g, 0), 0
        ListBox2.RemoveItem (g)
    End If
Next g
End Sub

Bonjour,

Merci bcp pour votre réponse,

Par contre quand je veux supprimer mes choix , le macro bug

a

Re,

Sans fichier impossible de savoir...

[Édition]

Private Sub Supprimer_Click()
Dim g, gmax, n, m As Variant

gmax = ListBox2.ListCount - 1
For g = gmax To 0 Step -1
    If ListBox2.Selected(g) = True Then
        ListBox1.AddItem ListBox2.List(g, 0), 0
        ListBox2.RemoveItem (g)
    End If
Next g
End Sub

Bonjour ,

mon Fichier est en PJ.
l'objectif est de choisir les onglets et les colonnes à exporter dans un nouveau fichier à partir d'un userforme :

image
Rechercher des sujets similaires à "listbox listbbox2"