Bonjour à tous,
Je me permets de vous solliciter car dans le cadre de mon travail je tente de mettre en place un userform qui permettrait en fonction de filtre multicritères d'extraire des colonnes définis d'une base de donnée dynamique. Pour ce faire et étant débutant en la matière, je me suis appuyer sur les tuto boisgontiers ( que je remercie au passage).
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 Private Sub UserForm_Initialize() On Error Resume Next NomTableau = "Tableau1" TblBD = Range(NomTableau).Value NbCol = UBound(TblBD, 2) Set d = CreateObject("scripting.dictionary") For i = LBound(TblBD) To UBound(TblBD) d(TblBD(i, 9)) = "" Next i Me.ChoixListBox1.List = d.keys Set d = CreateObject("scripting.dictionary") For i = LBound(TblBD) To UBound(TblBD) d(TblBD(i, 6)) = "" Next i Me.ChoixListBox2.List = d.keys Set d = CreateObject("scripting.dictionary") d.comparemode = vbTextCompare For i = LBound(TblBD) To UBound(TblBD) d(TblBD(i, 10)) = "" Next i Me.ChoixListBox3.List = d.keys Me.ListBox1.ColumnCount = NbCol + 1 Me.ListBox1.List = TblBD Range(NomTableau).ClearFormats EnteteListBox 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 Sub EnteteListBox() x = Me.ListBox1.Left + 8 Y = Me.ListBox1.Top - 20 For c = 1 To NbCol Set Lab = Me.Controls.Add("Forms.Label.1") Lab.Caption = Range(NomTableau).Offset(-1).Item(1, c) Lab.ForeColor = vbBlack Lab.Top = Y Lab.Left = x Lab.Height = 24 Lab.Width = Range(NomTableau).Columns(c).Width * 1# x = x + Range(NomTableau).Columns(c).Width * 1 tempcol = tempcol & Range(NomTableau).Columns(c).Width * 1# & ";" Next c tempcol = tempcol On Error Resume Next Me.ListBox1.ColumnWidths = tempcol On Error GoTo 0 End Sub
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 Private Sub ChoixListBox1_change() Affiche End Sub Private Sub ChoixListBox2_change() Affiche End Sub Private Sub ChoixListBox3_change() Affiche 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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 Sub Affiche() Set dchoisis1 = CreateObject("Scripting.Dictionary") For i = 0 To Me.ChoixListBox1.ListCount - 1 If Me.ChoixListBox1.Selected(i) Then dchoisis1(Me.ChoixListBox1.List(i, 0)) = "" Next i Set dchoisis2 = CreateObject("Scripting.Dictionary") For i = 0 To Me.ChoixListBox2.ListCount - 1 If Me.ChoixListBox2.Selected(i) Then dchoisis2(Me.ChoixListBox2.List(i, 0)) = "" Next i Set dchoisis3 = CreateObject("Scripting.Dictionary") For i = 0 To Me.ChoixListBox3.ListCount - 1 If Me.ChoixListBox3.Selected(i) Then dchoisis3(Me.ChoixListBox3.List(i, 0)) = "" Next i n = 0: Dim Liste() For i = LBound(TblBD) To UBound(TblBD) tmp = TblBD(i, 9) tmp2 = TblBD(i, 6) tmp3 = TblBD(i, 10) If (dchoisis1.exists(tmp) Or dchoisis1.Count = 0) _ And (dchoisis2.exists(tmp2) Or dchoisis2.Count = 0) _ And (dchoisis3.exists(tmp3) Or dchoisis3.Count = 0) Then n = n + 1 ReDim Preserve Liste(1 To NbCol + 1, 1 To n) For k = 1 To NbCol Liste(k, n) = TblBD(i, k) Next k Liste(k, n) = i End If Next i If n > 0 Then Me.ListBox1.Column = Liste Range(NomTableau).ClearFormats For i = 0 To Me.ListBox1.ListCount - 1 ligne = Me.ListBox1.List(i, NbCol) Range(NomTableau).Cells(ligne, 1).Resize(, NbCol).Interior.ColorIndex = 4 Next i Else Me.ListBox1.Clear End If Me.txtnbreco.Value = Me.ListBox1.ListCount end sub
Si les lignes de code supra fonctionne correctement, à ce stade j'ai un problème (erreur d'incompatibilité 13) lorsque je veux extraire les données apparaissant dans ma listbox (résultant de mes précédents filtres) dans un nouvel onglet. Voici la ligne de code qui cause le problème (erreur sur la ligne en gras) :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 Private Sub recu_Click() Application.ScreenUpdating = False Set f = Sheets("EXTRACTION") n = ListBox1.ListCount Tbl = Me.ListBox1.List f.[A2].Resize(100000, NbCol + 1).ClearContents f.[A2].Resize(n, 13) = Application.Index(Tbl, Evaluate("Row(1:" & n & ")"), Array(1, 3, 6, 2, 4, 5, 7, 8, 9, 10, 11, 12, 13)) End Sub
Si l'un d'entre vous pouvait m'expliquer comment corriger cette erreur ou adapter le code au besoin, je lui en serais très reconnaissant.
Je vous remercie d'avance ;
Partager