monter et descendre une liste

  • Initiateur de la discussion Initiateur de la discussion Chezmoi
  • Date de début Date de début

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

C

Chezmoi

Guest
Bonjour forum,

J'ai une liste box, avec des elements dedans. J'ai créer des boutons pour monter ou descendre l'éléement selectionné dans la liste. Mais problème je sais pas comment faire vous pouvez maider ?

merci davance
 
Bonjour,

Si on considère ton UserForm avec 3 objets :
- une liste nommée Liste
- un bouton nommé btn_descendre
- un bouton nommé btn-remonter

Tu peux gèrer le déplacement au sein de ta liste avec les évennements CLIC de chacun des 2 boutons. Ca donnera en code :

Private Sub btn_descendre_Click()
Pos = Liste.ListIndex
If Pos = Liste.ListCount - 1 Then Pos = Liste.ListCount - 2
Liste.ListIndex = Pos + 1
End Sub

Private Sub btn_remonter_Click()
Pos = Liste.ListIndex
If Pos < 1 Then Pos = 1
Liste.ListIndex = Pos - 1
End Sub

Et en plus ça s'adapte automatiquement à ta liste et son nombre d'éléments ... Sympa, non ? ;o))


Bonne continuation

Rai
 
Bonsoir tout le monde,

Voici une une autre proposition (sans USF) :

Private Sub CommandButton1_Click()
'Reculer un élément de la liste
Dim Temp As String
If ListBox1.ListCount = 0 Then Exit Sub
Temp = ListBox1.Text
If ListBox1.ListIndex <= 0 Then
ListBox1.List(0) = ListBox1.List(ListBox1.ListCount - 1)
ListBox1.List(ListBox1.ListCount - 1) = Temp
ListBox1.ListIndex = ListBox1.ListCount - 1
Else
ListBox1.List(ListBox1.ListIndex) = ListBox1.List(ListBox1.ListIndex - 1)
ListBox1.List(ListBox1.ListIndex - 1) = Temp
ListBox1.ListIndex = ListBox1.ListIndex - 1
End If
End Sub

Private Sub CommandButton2_Click()
'Avancer un élément de la liste
Dim Temp As String
If ListBox1.ListCount = 0 Then Exit Sub
Temp = ListBox1.Text
If ListBox1.ListIndex = ListBox1.ListCount - 1 Then
ListBox1.List(ListBox1.ListIndex) = ListBox1.List(0)
ListBox1.List(0) = Temp
ListBox1.ListIndex = 0
Else
ListBox1.List(ListBox1.ListIndex) = ListBox1.List(ListBox1.ListIndex + 1)
ListBox1.List(ListBox1.ListIndex + 1) = Temp
ListBox1.ListIndex = ListBox1.ListIndex + 1
End If
End Sub

Pour que cela marche, il ne faut pas que la zone de liste soit remplie avec sa propriété "ListFillRange", il faut utiliser la méthode "AddItem".
Par exemple comme ceci :

Private Sub Workbook_Open()
Dim I As Integer
Feuil3.ListBox1.Clear
For I = 1 To 7
Feuil3.ListBox1.AddItem Feuil3.Cells(I, 1).Text
Next
Feuil3.ListBox1.ListIndex = 0
End Sub

A+
Horatio
 
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD
Assurez vous de marquer un message comme solution pour une meilleure transparence.

Discussions similaires

Réponses
11
Affichages
248
  • Question Question
Microsoft 365 Carte géogrpahique
Réponses
6
Affichages
245
  • Question Question
Microsoft 365 Listbox
Réponses
3
Affichages
492
Réponses
3
Affichages
372
Réponses
1
Affichages
124
Réponses
10
Affichages
334
Réponses
14
Affichages
478
Retour