Ajout d'affichage de colonne dans une listbox

mavean

XLDnaute Junior
Bonjour, j'utilise le code suivant (copié sur le forum)
Cela marche impec et l'affichage dans la ListBox ce fait bien.
Mais j'aimerais qu'en plus de m'afficher les colonne C et D il m'affiche la colonne E et H (sur la même ligne)
Si possible en m’expliquant pour que je comprenne encore merci à tous les contributeurs.

Private Sub TextBox1_Change()
i = 0
Set plage = Sheets("BD").Range("C3:C" & Sheets("BD").Range("C65536").End(xlUp).Row)
Set plage_a = Sheets("BD").Range("C3:D" & Sheets("BD").Range("D65536").End(xlUp).Row)
NbCol = plage.Columns.Count
Me.ListBox1.Clear
Set c = plage_a.Find(Me.TextBox1, , , xlPart)
If Not c Is Nothing Then
premier = c.Address
Do
Me.ListBox1.AddItem
lig = c.Row - plage.Row + 1
For col = 1 To NbCol
Me.ListBox1.List(i, col - 1) = plage.Cells(lig, col)
Me.ListBox1.List(ListBox1.ListCount - 1, 1) = plage.Cells(lig, col + 1)
Next col
i = i + 1
Set c = plage_a.FindNext(c)
Loop While Not c Is Nothing And c.Address <> premier
End If
If Me.TextBox1 = "" Then Call Initlistbox1
End Sub
Sub Initlistbox1()
With ListBox1
.Clear
.ColumnCount = 2
.ColumnWidths = "80;200"
End With

With Sheets("BD")
If .Range("B3") <> "" Then
Lg = .Range("B65536").End(xlUp).Row
End If

For Mtg = 3 To Lg
If .Cells(Mtg, 3).Value <> "" Then
ListBox1.AddItem .Cells(Mtg, 3)
ListBox1.List(ListBox1.ListCount - 1, 1) = .Cells(Mtg, 4)
End If
Next Mtg

ListBox1.ListIndex = -1
If ListBox1.ListCount > 0 Then ListBox1.ListIndex = 0
End With
End Sub
 

Paf

XLDnaute Barbatruc
Re : Ajout d'affichage de colonne dans une listbox

Bonjour,

Si j'ai bien compris :

ListBox1.List(N° item, N° colonne) = Valeur N° item et N° colonne commençant à 0

Code:
ListBox1.AddItem .Cells(Mtg, 3) ' on crée un nouvel item
ListBox1.List(ListBox1.ListCount - 1, 1) = .Cells(Mtg, 4) ' ListBox1.ListCount - 1 donne le n° item en cours
ListBox1.List(ListBox1.ListCount - 1, 2) = .Cells(Mtg, 5)
ListBox1.List(ListBox1.ListCount - 1, 3) = .Cells(Mtg, 8)

Dans les propriétés ou par code modifier le nombre de colonnes de la listbox1

A+
 

mavean

XLDnaute Junior
Re : Ajout d'affichage de colonne dans une listbox

Re
J'ai changé le nb d colonnes : ColumnCount = 4
j'ai dimensionné les colonnes : ColumnWidths = "80;200;40;40"
Et ajouté
ListBox1.List(ListBox1.ListCount - 1, 2) = .Cells(Mtg, 5)
ListBox1.List(ListBox1.ListCount - 1, 3) = .Cells(Mtg, 8)

Mais je n'ai toujours que 2 colonnes dans ma listbox
Que faut il encore ajouter?

Sub Initlistbox1()
With ListBox1
.Clear
.ColumnCount = 4
.ColumnWidths = "80;200;40;40"
End With

With Sheets("BD")
If .Range("B3") <> "" Then
Lg = .Range("B65536").End(xlUp).Row
End If

For Mtg = 3 To Lg
If .Cells(Mtg, 3).Value <> "" Then
ListBox1.AddItem .Cells(Mtg, 3)
ListBox1.List(ListBox1.ListCount - 1, 1) = .Cells(Mtg, 4)
ListBox1.List(ListBox1.ListCount - 1, 2) = .Cells(Mtg, 5)
ListBox1.List(ListBox1.ListCount - 1, 3) = .Cells(Mtg, 8)
End If
Next Mtg

ListBox1.ListIndex = -1
If ListBox1.ListCount > 0 Then ListBox1.ListIndex = 0
End With
End Sub
 

mavean

XLDnaute Junior
Re : Ajout d'affichage de colonne dans une listbox

Re
Non les colonnes ne sont pas vide mais malgré mes essais pas de résultat, je remets le code ci dessous.
Merci des vos aides pour me dire ce qu'il cloche

Private Sub TextBox1_Change()
i = 0
Set plage = Sheets("F2").Range("C3:C" & Sheets("F2").Range("C65536").End(xlUp).Row)
Set plage_a = Sheets("F2").Range("C3:D" & Sheets("F2").Range("D65536").End(xlUp).Row)

NbCol = plage.Columns.Count

Me.ListBox1.Clear
Set c = plage_a.Find(Me.TextBox1, , , xlPart)
If Not c Is Nothing Then
premier = c.Address
Do
Me.ListBox1.AddItem
lig = c.Row - plage.Row + 1
For col = 1 To NbCol
Me.ListBox1.List(i, col - 1) = plage.Cells(lig, col)
Me.ListBox1.List(ListBox1.ListCount - 1, 1) = plage.Cells(lig, col + 1)
Next col
i = i + 1
Set c = plage_a.FindNext(c)
Loop While Not c Is Nothing And c.Address <> premier
End If
If Me.TextBox1 = "" Then Call Initlistbox1
End Sub
Sub Initlistbox1()
With ListBox1
.Clear
.ColumnCount = 4
.ColumnWidths = "80;270;40;40"
End With

With Sheets("F2")
If .Range("B3") <> "" Then
Lg = .Range("B65536").End(xlUp).Row
End If

For Mtg = 3 To Lg
If .Cells(Mtg, 8).Value <> "" Then
ListBox1.AddItem .Cells(Mtg, 3)
ListBox1.List(ListBox1.ListCount - 1, 1) = .Cells(Mtg, 4)
ListBox1.List(ListBox1.ListCount - 1, 2) = .Cells(Mtg, 5)
ListBox1.List(ListBox1.ListCount - 1, 3) = .Cells(Mtg, 8)
End If
Next Mtg

ListBox1.ListIndex = -1
If ListBox1.ListCount > 0 Then ListBox1.ListIndex = 0
End With
End Sub
 

Paf

XLDnaute Barbatruc
Re : Ajout d'affichage de colonne dans une listbox

re,

Sans le classeur impossible de vérifier .
Si deux colonnes de la listbox sont remplies, pourquoi pas les quatre ? à moins qu'on ne passe pas dans cette partie du code ! puisqu'en reprenant le code des post #1 et #5 on voit qu'il y a deux mises à jour de la Listbox1:
Code:
For col = 1 To NbCol
     Me.ListBox1.List(i, col - 1) = plage.Cells(lig, col)
     Me.ListBox1.List(ListBox1.ListCount - 1, 1) = plage.Cells(lig, col + 1)
Next col
et
Code:
For Mtg = 3 To Lg
    If .Cells(Mtg, 8).Value <> "" Then
        ListBox1.AddItem .Cells(Mtg, 3)
        ListBox1.List(ListBox1.ListCount - 1, 1) = .Cells(Mtg, 4)
        ListBox1.List(ListBox1.ListCount - 1, 2) = .Cells(Mtg, 5)
        ListBox1.List(ListBox1.ListCount - 1, 3) = .Cells(Mtg, 8)
    End If
Next Mtg
Avec des précisions sur ce qu'est censé faire le code et le classeur pour tester, ce serait plus facile que d'éplucher un code non indenté sans savoir ce qu'il devrait faire.

A+
 

mavean

XLDnaute Junior
Re : Ajout d'affichage de colonne dans une listbox

Re

Dans la TextBox 1 je tape soit un code (colonne C) soit une désignation (colonne D)

Et ensuite si le code ou la désignation existe dans la list box j'ai l'affichage de ce qui correspond
Soit une ligne car il n'y a pas 2 codes de différents soit plusieurs ligne si la désignation s'applique a des codes différents
et
sur cette ligne 2 colonne
La C et la D alors que je souhaite la C / D / E / H

Merci de votre aide
 

mavean

XLDnaute Junior
Re : Ajout d'affichage de colonne dans une listbox

Bonjour

J'ai compris que sans le fichier c'est difficile de répondre.
Je le joint donc en ayant supprimer d'autres feuilles plus confidentielle.
Donc dans le classeur il y a d'autres feuilles (importance de nommer la feuille F2)
J'ai réexpliqué ce que je souhaite à droite de la feuille.
L'userform c'est le 2 car dans le classeur j'ai aussi un Userform1.
Encore Merci
 

Pièces jointes

  • FICHIER POUR RENTRER LES STOCKS POUR FORUM.xlsm
    83.8 KB · Affichages: 44

Paf

XLDnaute Barbatruc
Re : Ajout d'affichage de colonne dans une listbox

re,

Vous avez donc bien deux endroits où vous alimentez la listbox1 dans :
Private Sub TextBox1_Change()
Sub Initlistbox1() appelée par Private Sub UserForm_initialize()

Dans Sub Initlistbox1() on devrait alimenter la listbox1 par une boucle For Mtg = 3 To Lg . Or pour déterminer Lg on utilise:
Code:
         If .Range("B3") <> "" Then
            Lg = .Range("B65536").End(xlUp).Row
        End If

et comme B3 est vide Lg n'est pas initialisée (donc =0) et la boucle For Mtg = 3 To Lg ne se déclenche pas.

Ce qui ne semble pas gênant puisque c'est la saisie en TextBox1 qui alimente la listbox avec les articles contenant le texte saisi.

On peut donc supprimer Sub Initlistbox1() et son appel dans Private Sub UserForm_initialize()

Ne restant plus qu'une source d'alimentation ( dans Private Sub TextBox1_Change()), c'est là qu'il faut rajouter l'alimentation des colonnes supplémentaires:

Code:
                 For col = 1 To NbCol
                   Me.ListBox1.List(i, col - 1) = plage.Cells(lig, col)
                   Me.ListBox1.List(ListBox1.ListCount - 1, 1) = plage.Cells(lig, col + 1)
                   Me.ListBox1.List(ListBox1.ListCount - 1, 2) = plage.Cells(lig, col + 2) 'à adapter
                   Me.ListBox1.List(ListBox1.ListCount - 1, 3) = plage.Cells(lig, col + 3) ' à adapter
                 Next col

A+
 

Discussions similaires

Réponses
4
Affichages
231
Réponses
17
Affichages
902

Statistiques des forums

Discussions
312 386
Messages
2 087 849
Membres
103 668
dernier inscrit
Aekhassen