Bonjour, je viens demander votre aide car j'aimerai pouvoir sélectionner les 4 derniers items d'une listbox placée dans un userform.
Je souhaiterai que la sélection des 4 derniers se fasse automatiquement, en quelque sorte que l'on puisse activer un bouton, déclenchant l'userform sur une autre page (que l'on ne voit pas), et que tout se fasse en un clic en fait.
Dans ce userform, j'ai 2 boutons (Valider et Annuler(CommandButton2)) + une listbox multiselect.
Mon code vous paraitra certainement un peu spécial mais chaque item coché permet d'afficher une colonne dans un TCD.
J'ai regardé à de nombreux endroits, comme ici sur les forums pour trouver des infos mais je n'arrive pas à adapter les différents codes.
Voici mon code:
Partie Valider_Click:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 Private Sub valider_click() Dim X As Integer Dim i As Integer Dim sellist As Integer Application.ScreenUpdating = False 'désactive la mise à jour de l'écran sellist = 0 For i = 0 To ListBox1.ListCount - 1 If ListBox1.Selected(i) = True Then sellist = 1 End If Next i If sellist = 1 Then ' on place tous à true With Sheets("Calculs").PivotTables("PivotTable4").PivotFields("Date Opérations") For i = 0 To ListBox1.ListCount - 1 If ListBox1.List(i) <> "" Then .PivotItems(CStr(ListBox1.List(i))).Visible = True End If Next i For i = 0 To ListBox1.ListCount - 1 If ListBox1.List(i) <> "" Then If ListBox1.Selected(i) = False Then .PivotItems(CStr(ListBox1.List(i))).Visible = False Else .PivotItems(CStr(ListBox1.List(i))).Visible = True End If End If Next i End With Call Calcul.Calculs_module1 Else MsgBox ("Il faut choisir au moins une date.") End If Unload Me Application.ScreenUpdating = True 'active la mise à jour de l'écran End Sub
Partie Annuler_CLick:
Partie permettant de définir le nombre max d'items à sélectionner:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 Private Sub CommandButton2_Click() Dim i As Integer For i = 0 To ListBox1.ListCount - 1 If ListBox1.Selected(i) = True Then ListBox1.Selected(i) = False Next i Valider.Visible = False Unload Me End Sub
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 Private Sub ListBox1_Change() 'http://www.developpez.net/forums/d725485/logiciels/microsoft-office/excel/macros-vba-excel/limiter-nombre-selection-listbox/ ' / Ucfoutu Static nb As Integer Dim choisi As Integer, max As Integer max = 6 ' tu mets ici la valeur que tu veux (nombre maxi de sélections autorisées) choisi = ListBox1.ListIndex If ListBox1.Selected(choisi) = False Then nb = nb - 1: Exit Sub If nb >= max Then ListBox1.Selected(choisi) = False nb = nb + 1 '''' If nb > 0 Then Valider.Visible = True Else Valider.Visible = False End If End Sub
Partie d'initialisation du Userform:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 Private Sub UserForm_Initialize() Dim MonDico As Object, Cellule As Range Application.ScreenUpdating = False 'désactive la mise à jour de l'écranMsgBox ListBox1.List '*** on note les analytiques présents ds feuil1 Set MonDico = CreateObject("Scripting.Dictionary") For Each Cellule In Sheets("Prévisions").Range("A2:A65536") If Not MonDico.Exists(Cellule.Value) Then MonDico.Add Cellule.Value, Cellule.Value Next Cellule ListBox1.List = MonDico.Items MonDico.RemoveAll End Sub
Partager