Alimenter ListBox avec un Array

Bonjour,

J'ai un problème que je n'arrive pas à résoudre, je souhaite alimenter une listbox avec un tableau() , j'y arrive.

Le problème est que je souhaite que la listbox ne soit remplit qu'avec les lignes du tableaux ayant pour critère un nom de liste contenu dans une combobox, la aussi j'ai réussi sauf que le tableau retourné supprime bien les lignes ou le critère n'est pas remplit, mais il met des lignes vide à la place.

capture

Je voudrais enlever les lignes vides, voici le code en question :

Sub Alimenter_lstbListeProspection()

Dim TableauBase(), TabLstProspection()
Dim NbrLgn As Integer, NbrColonne As Integer, i As Integer, j As Integer, k As Variant, n As Integer

NbrLgn = Feuil2.Range("A" & Rows.Count).End(xlUp).Row
NbrColonne = Feuil2.Cells(1, Columns.Count).End(xlToLeft).Column

TableauBase = Feuil2.Range(Feuil2.Cells(2, 1), Feuil2.Cells(NbrLgn, NbrColonne))

ReDim TabLstProspection(1 To UBound(TableauBase), 1 To 5)
j = 0

For Each k In Array(1, 3, 4, 8, 10)
    j = j + 1
    For i = 1 To UBound(TableauBase)
        If TableauBase(i, 2) = UserForm1.cb_NomListe.Text Then
        TabLstProspection(i, j) = TableauBase(i, k)
        End If
    Next i
Next k

UserForm1.lstbx_ListeProspection.List = TabLstProspection

End Sub

Merci,

24listview.xlsm (42.82 Ko)

Bonjour,

Un essai ...

Nouveau code pour le module " AlimenterCombobox " ... changements au bas ...

Sub Alimenter_lstbListeProspection()

Dim TableauBase(), TabLstProspection()
Dim NbrLgn As Integer, NbrColonne As Integer, i As Integer, j As Integer, k As Variant, n As Integer

    NbrLgn = Feuil2.Range("A" & Rows.Count).End(xlUp).Row
    NbrColonne = Feuil2.Cells(1, Columns.Count).End(xlToLeft).Column
    TableauBase = Feuil2.Range(Feuil2.Cells(2, 1), Feuil2.Cells(NbrLgn, NbrColonne))
    ReDim TabLstProspection(1 To UBound(TableauBase), 1 To 5)
    j = 0

    For Each k In Array(1, 3, 4, 8, 10)
        j = j + 1

        For i = 1 To UBound(TableauBase)
            If TableauBase(i, 2) = UserForm1.cb_NomListe.Text Then
                TabLstProspection(i, j) = TableauBase(i, k)
            End If
        Next i
    Next k

    UserForm1.lstbx_ListeProspection.List = TabLstProspection

'' <<< >>> les changements commencent ici ...
    For i = UserForm1.lstbx_ListeProspection.ListCount - 1 To 0 Step -1
        If UserForm1.lstbx_ListeProspection.List(i) = "" Then UserForm1.lstbx_ListeProspection.RemoveItem (i)
    Next i
End Sub

ric

Bonjour,

Ouupppssss! ... ça fonctionne bien s'il y a plus d'une ligne ... sinon ...

Je vais chercher le souci ...

ric

Bonjour,

J'ai modifié le code dans mon post précédent ... ça fonctionne mieux ...

ric

Bonjour,

Comme j'ai travaillé sur le sujet hier, le code .. dropdownlist est exécuté deux fois. Une fois en cliquant sur la flèche de la combobox cb_NomListe et une deuxième fois lors du choix dans la liste. Je n'ai pas compris le pourquoi du choix

Sinon une autre méthode sans array

Sub Alimenter_lstbListeProspection()

Dim NbrLgn As Integer, NbrColonne As Integer
Dim cel As Range

UserForm1.lstbx_ListeProspection.Clear
For Each cel In Feuil2.ListObjects("Tableau2").ListColumns(2).DataBodyRange
    If cel = UserForm1.cb_NomListe.text Then
        With UserForm1.lstbx_ListeProspection
            .AddItem
            .List(.ListCount - 1, 0) = cel.Value
            .List(.ListCount - 1, 1) = cel.Offset(0, 1)
            .List(.ListCount - 1, 2) = cel.Offset(0, 2)
            .List(.ListCount - 1, 3) = cel.Offset(0, 6)
            .List(.ListCount - 1, 4) = cel.Offset(0, 8)
        End With
    End If
Next cel
End Sub

Cordialement

Bonjour,

J'ai modifié le code dans mon post précédent ... ça fonctionne mieux ...

ric

Bonjour ric,

C'est parfait ! Merci beaucoup, j'ai essayé pas mal de chose avec plusieurs tableaux mais ta solution simple est bien plus lisible et pratique :)

Bonjour,

Comme j'ai travaillé sur le sujet hier, le code .. dropdownlist est exécuté deux fois. Une fois en cliquant sur la flèche de la combobox cb_NomListe et une deuxième fois lors du choix dans la liste. Je n'ai pas compris le pourquoi du choix

Sinon une autre méthode sans array

Sub Alimenter_lstbListeProspection()

Dim NbrLgn As Integer, NbrColonne As Integer
Dim cel As Range

UserForm1.lstbx_ListeProspection.Clear
For Each cel In Feuil2.ListObjects("Tableau2").ListColumns(2).DataBodyRange
    If cel = UserForm1.cb_NomListe.text Then
        With UserForm1.lstbx_ListeProspection
            .AddItem
            .List(.ListCount - 1, 0) = cel.Value
            .List(.ListCount - 1, 1) = cel.Offset(0, 1)
            .List(.ListCount - 1, 2) = cel.Offset(0, 2)
            .List(.ListCount - 1, 3) = cel.Offset(0, 6)
            .List(.ListCount - 1, 4) = cel.Offset(0, 8)
        End With
    End If
Next cel
End Sub

Cordialement

Bonjour Dan,

Merci pour ton aide également ! J'ai utilisé dropdownlist car je veux que la listbox s'actualise quand je change de nom dans la combobox

Rechercher des sujets similaires à "alimenter listbox array"