XL 2010 Impossible de lire la propriété .List

cathodique

XLDnaute Barbatruc
Bonsoir,

J'ai été grandement aidé par Job75 (Que je remercie et salue;)) [cette discussion].
Je rencontre le problème cité en titre. La listbox a 2 colonnes dont l'une n'est pas visible (largeur=0).
Cette colonne contient le chemin complet du fichier à récupérer le contenu dans la listbox2.
Code qui coince ci-dessous
VB:
Private Sub ListBox1_Click()
    Dim TextLine As String
    Dim myFile As String
    Dim IdFile As Integer
    Repertoire = ListBox1.List(ListBox1.ListIndex, 1) ''***
    If Me.ListBox1.ListCount <> -1 Then
        Me.TextBox1 = ListBox1.List(ListBox1.ListIndex, 0)  '***Texte de l’item sélectionné dans listbox1
        myFile = Repertoire
    End If

    IdFile = FreeFile
    Open myFile For Input As #IdFile
    Do While Not EOF(IdFile)
        Line Input #IdFile, TextLine
        ListBox2.AddItem (TextLine)
    Loop
    Close #IdFile
End Sub

Avec mes remerciements anticipés.

Bonne soirée
 

Pièces jointes

  • Archive2_VBA - Copie.xlsm
    38.1 KB · Affichages: 8
Solution
Bonjour cathodique, le forum,
Mais alors, comment récupérer l'item sélectionné dans la textbox?
En utilisant la variable mémorisée flag qui bloque TextBox1_Change :
Code:
Private Sub TextBox1_Change() 'filtre listbox via textbox
    If flag Then Exit Sub
    'If liste(1, 1) = "" Then Exit Sub
    Dim critere$, i&, a$(), n&
    critere = "*" & LCase(Trim(TextBox1)) & "*"
    For i = 1 To UBound(liste, 2)
        If LCase(liste(2, i)) Like critere Then
            n = n + 1
            ReDim Preserve a(1 To 2, 1 To n)
            a(1, n) = liste(1, i)
            a(2, n) = liste(2, i)
        End If
    Next
    ListBox2.Clear 'RAZ
    ListBox1.Clear 'RAZ
    If n Then If n > 1 Then ListBox1.List = Application.Transpose(a) Else...

cathodique

XLDnaute Barbatruc
Bonjour Job75;), le forum,

@job75 : Je ne vous remercierai jamais assez pour votre aide.

:confused: Vraiment confus pour mes grossières erreurs. En effet, ListBox1.ListCount n'est jamais égal à -1

En suivant tes indications le code ne plante plus. Mais alors, comment récupérer l'item sélectionné dans la textbox?

Merci beaucoup. Bonne journée.
 
Dernière édition:

job75

XLDnaute Barbatruc
Bonjour cathodique, le forum,
Mais alors, comment récupérer l'item sélectionné dans la textbox?
En utilisant la variable mémorisée flag qui bloque TextBox1_Change :
Code:
Private Sub TextBox1_Change() 'filtre listbox via textbox
    If flag Then Exit Sub
    'If liste(1, 1) = "" Then Exit Sub
    Dim critere$, i&, a$(), n&
    critere = "*" & LCase(Trim(TextBox1)) & "*"
    For i = 1 To UBound(liste, 2)
        If LCase(liste(2, i)) Like critere Then
            n = n + 1
            ReDim Preserve a(1 To 2, 1 To n)
            a(1, n) = liste(1, i)
            a(2, n) = liste(2, i)
        End If
    Next
    ListBox2.Clear 'RAZ
    ListBox1.Clear 'RAZ
    If n Then If n > 1 Then ListBox1.List = Application.Transpose(a) Else ListBox1.AddItem a(1, 1): ListBox1.List(0, 1) = a(2, 1)
End Sub

Private Sub ListBox1_Click()
    Dim myFile, IdFile As Integer, TextLine As String
    myFile = ListBox1.List(ListBox1.ListIndex, 1)
    flag = True 'bloque TextBox1_Change
    TextBox1 = ListBox1
    flag = False
    ListBox2.Clear 'RAZ
    IdFile = FreeFile
    Open myFile For Input As #IdFile
    Do While Not EOF(IdFile)
        Line Input #IdFile, TextLine
        ListBox2.AddItem TextLine
    Loop
    Close #IdFile
End Sub
A+
 

Pièces jointes

  • MonDossier.zip
    26.7 KB · Affichages: 2

cathodique

XLDnaute Barbatruc
Bonjour cathodique, le forum,

En utilisant la variable mémorisée flag qui bloque TextBox1_Change :
Code:
Private Sub TextBox1_Change() 'filtre listbox via textbox
    If flag Then Exit Sub
    'If liste(1, 1) = "" Then Exit Sub
    Dim critere$, i&, a$(), n&
    critere = "*" & LCase(Trim(TextBox1)) & "*"
    For i = 1 To UBound(liste, 2)
        If LCase(liste(2, i)) Like critere Then
            n = n + 1
            ReDim Preserve a(1 To 2, 1 To n)
            a(1, n) = liste(1, i)
            a(2, n) = liste(2, i)
        End If
    Next
    ListBox2.Clear 'RAZ
    ListBox1.Clear 'RAZ
    If n Then If n > 1 Then ListBox1.List = Application.Transpose(a) Else ListBox1.AddItem a(1, 1): ListBox1.List(0, 1) = a(2, 1)
End Sub

Private Sub ListBox1_Click()
    Dim myFile, IdFile As Integer, TextLine As String
    myFile = ListBox1.List(ListBox1.ListIndex, 1)
    flag = True 'bloque TextBox1_Change
    TextBox1 = ListBox1
    flag = False
    ListBox2.Clear 'RAZ
    IdFile = FreeFile
    Open myFile For Input As #IdFile
    Do While Not EOF(IdFile)
        Line Input #IdFile, TextLine
        ListBox2.AddItem TextLine
    Loop
    Close #IdFile
End Sub
A+
Bonjour Job75;););););),

Yes! à la perfection:cool:. Très très fort. Je vous le redis, je ne vous remercierai jamais assez.
Je savais qu'il fallait utiliser une variable booléenne. Mais j'avoue que depuis 7h, j'essaie mais en vain.
Encore merci. Là, je peux dire que j'ai mon petit fichier pour retrouver plus facilement mes bouts de code.

Toute ma gratitude et toute ma reconnaissance.
Je vous souhaite une excellente journée.
 

job75

XLDnaute Barbatruc
Bonjour cathodique, le forum,

J'oubliais, il faut terminer UserForm_Initialize comme TextBox1_Change :
VB:
    ListBox2.Clear 'RAZ
    ListBox1.Clear 'RAZ
    If n Then If n > 1 Then ListBox1.List = Application.Transpose(liste) Else ListBox1.AddItem liste(1, 1): ListBox1.List(0, 1) = liste(2, 1)
A+
 

Pièces jointes

  • MonDossier.zip
    27 KB · Affichages: 6

cathodique

XLDnaute Barbatruc
Bonjour cathodique, le forum,

J'oubliais, il faut terminer UserForm_Initialize comme TextBox1_Change :
VB:
    ListBox2.Clear 'RAZ
    ListBox1.Clear 'RAZ
    If n Then If n > 1 Then ListBox1.List = Application.Transpose(liste) Else ListBox1.AddItem liste(1, 1): ListBox1.List(0, 1) = liste(2, 1)
A+
Bonsoir Job75,

Désolé, de répondre tardivement.
Merci beaucoup pour ta disponibilité et ton aide.

Bonne soirée
 

Discussions similaires

Statistiques des forums

Discussions
311 729
Messages
2 081 966
Membres
101 852
dernier inscrit
dthi16088