IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

Actualisation automatique d'une ListBox


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2015
    Messages
    56
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gers (Midi Pyrénées)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2015
    Messages : 56
    Par défaut Actualisation automatique d'une ListBox
    Bonjour,

    Je cherche en ce moment à réaliser une macro pour générer des plannings de bénévoles automatiquement et je cherche à développer une interface par userform. Les postes sont prédéfinis et j'ai créé une USF "Préférences" dans laquelle il est possible de supprimer, modifier ou ajouter un poste prédéfini. La sélection du poste à modifier ou à supprimer se fait par une listbox. Seulement, il faudrait que cette listbox s'actualise après la modification des valeurs. J'ai eu beau chercher, je n'arrive pas à le faire.. Auriez vous une solution à me proposer?

    Voici une image pour illustrer :
    Nom : Capture.JPG
Affichages : 1887
Taille : 76,2 Ko

    Le tableau à gauche est le tableau qui est modifié par les command buttons.

    Et voici le code pour l'initialisation de la listbox et les sub qui permettent d'effectuer les modifications :
    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
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
     
    Private Sub userform_initialize()
     
        'Initialisation listbox1
        Dim Cell As Range
        Dim Unique As New Collection
        Dim Valeur As Range
        Dim i As Integer
        ListBox1.ColumnCount = 2
     
        On Error Resume Next
        For Each Cell In Range("Pen_Postes")
            Unique.Add Cell, CStr(Cell)
        Next Cell
        On Error GoTo 0
        For Each Valeur In Unique
            Me.ListBox1.AddItem Valeur
        Next Valeur
     
        For i = 0 To ListBox1.ListCount - 1
            ListBox1.List(i, 1) = Range("Pen_Pen").Cells(i + 1, 1)
        Next i
        '----------------------
     
    Private Sub CommandButton1_Click()
        réponse = MsgBox("Es tu sûr(e) de vouloir supprimer ce poste?", vbYesNo + vbExclamation)
     
        If réponse = vbNo Then Exit Sub
     
        Range("Pen_Postes").Cells(Me.ListBox1.ListIndex + 1, 1).EntireRow.delete shift:=xlUp
     
        With Sheets("Description Poste")
                .AutoFilterMode = False
                .Range("B2:D2").AutoFilter
                .AutoFilter.Sort.SortFields.Clear
                .AutoFilter.Sort.SortFields.Add Key:=Range("B2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        End With
        With ActiveWorkbook.Worksheets("Description Poste").AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
     
    End Sub
     
    Private Sub CommandButton2_Click()
        If TextBox1.Value = "" Or TextBox2.Value = "" Then
            MsgBox "Merci de définir correctement le poste", vbInformation + vbOKOnly
            Exit Sub
        End If
     
        If Not IsNumeric(TextBox2.Value) Or TextBox2.Value > 4 Or TextBox2.Value < 1 Then
            MsgBox "La pénibilité doit être un chiffre compris entre 1 et 4. La valeur 0 correspond à une pause", vbInformation + vbOKOnly
            Exit Sub
        End If
     
        Range("Pen_Postes").Cells(Me.ListBox1.ListIndex + 1, 1) = TextBox1.Value
        Range("Pen_Pen").Cells(Me.ListBox1.ListIndex + 1, 1) = TextBox2.Value
        Range("Pen_Com").Cells(Me.ListBox1.ListIndex + 1, 1) = TextBox3.Value
     
        With Sheets("Description Poste")
                .AutoFilterMode = False
                .Range("B2:D2").AutoFilter
                .AutoFilter.Sort.SortFields.Clear
                .AutoFilter.Sort.SortFields.Add Key:=Range("B2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        End With
        With ActiveWorkbook.Worksheets("Description Poste").AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
     
    End Sub
     
    Private Sub CommandButton3_Click()
        If Not IsNumeric(TextBox2.Value) Or TextBox2.Value > 4 Or TextBox2.Value < 1 Then
            MsgBox "La pénibilité doit être un chiffre compris entre 1 et 4. La valeur 0 correspond à une pause", vbInformation + vbOKOnly
            Exit Sub
        End If
     
        If TextBox1.Value = "" Or TextBox2.Value = "" Then
            MsgBox "Merci de définir correctement le poste", vbInformation + vbOKOnly
            Exit Sub
        End If
     
        n = Application.WorksheetFunction.CountA(Range("Pen_Postes"))
     
        Range("Pen_Postes").Cells(n + 1, 1) = TextBox1.Value
        Range("Pen_Pen").Cells(n + 1, 1) = TextBox2.Value
        Range("Pen_Com").Cells(n + 1, 1) = TextBox3.Value
     
        With Sheets("Description Poste")
                .AutoFilterMode = False
                .Range("B2:D2").AutoFilter
                .AutoFilter.Sort.SortFields.Clear
                .AutoFilter.Sort.SortFields.Add Key:=Range("B2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        End With
        With ActiveWorkbook.Worksheets("Description Poste").AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
     
    End Sub
     
    Private Sub ListBox1_Click()
        If Me.ListBox1.ListIndex < 0 Then
            MsgBox "Aucun poste n'est sélectionné", vbOKOnly + vbInformation
            Exit Sub
        End If
     
        TextBox1.Value = Range("Pen_Postes").Cells(Me.ListBox1.ListIndex + 1, 1)
        TextBox2.Value = Range("Pen_Pen").Cells(Me.ListBox1.ListIndex + 1, 1)
        TextBox3.Value = Range("Pen_Com").Cells(Me.ListBox1.ListIndex + 1, 1)
     
    End Sub
     
     
     
    End Sub
    Merci d'avance!

    Thomas

  2. #2
    Membre éclairé
    Avatar de tamtam64
    Homme Profil pro
    stagiaire developpement vba
    Inscrit en
    Mai 2012
    Messages
    456
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : stagiaire developpement vba
    Secteur : Finance

    Informations forums :
    Inscription : Mai 2012
    Messages : 456
    Billets dans le blog
    17
    Par défaut hey
    Salut à toi , comme je travail sur un projet similaire (userform) , tu devrais tout simplement utiliser une macro evenementielle , "change"
    si tu veux un peu d'ide n'hesite pas, je fonctionne comme ca:
    tu crait une liste boxe , que tu modifies tres bien , mais l'evenement change permet lorsque tu change le contenu , de pouvoir faire une action comme par exemple remettre a jour la liste issue de ton range , pour que les données s'affchent

  3. #3
    Membre éclairé
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2015
    Messages
    56
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gers (Midi Pyrénées)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2015
    Messages : 56
    Par défaut
    Salut,

    Entre temps j'ai trouvé une solution : j'ai rajouté un listbox.clear puis une initialisation de la listbox à la fin de mes commandbuttons_click. C'est moche mais ça fait ce que je lui demande ^^

    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
    Private Sub CommandButton1_Click()
        réponse = MsgBox("Es tu sûr(e) de vouloir supprimer ce poste?", vbYesNo + vbExclamation)
     
        If réponse = vbNo Then Exit Sub
     
        Range("Pen_Postes").Cells(Me.ListBox1.ListIndex + 1, 1).EntireRow.delete shift:=xlUp
     
        With Sheets("Description Poste")
                .AutoFilterMode = False
                .Range("B2:D2").AutoFilter
                .AutoFilter.Sort.SortFields.Clear
                .AutoFilter.Sort.SortFields.Add Key:=Range("B2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        End With
        With ActiveWorkbook.Worksheets("Description Poste").AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
     
    'Réinitialisation de la listbox
        ListBox1.Clear
        Dim Cell As Range
        Dim Unique As New Collection
        Dim Valeur As Range
        Dim i As Integer
        ListBox1.ColumnCount = 2
     
        On Error Resume Next
        For Each Cell In Range("Pen_Postes")
            Unique.Add Cell, CStr(Cell)
        Next Cell
        On Error GoTo 0
        For Each Valeur In Unique
            Me.ListBox1.AddItem Valeur
        Next Valeur
     
        For i = 0 To ListBox1.ListCount - 1
            ListBox1.List(i, 1) = Range("Pen_Pen").Cells(i + 1, 1)
        Next i
     
    End Sub
    Du coup je vais laisser comme ça mais je regarderai avec attention ta proposition pour un projet projet!
    Merci

  4. #4
    Membre éclairé
    Avatar de tamtam64
    Homme Profil pro
    stagiaire developpement vba
    Inscrit en
    Mai 2012
    Messages
    456
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : stagiaire developpement vba
    Secteur : Finance

    Informations forums :
    Inscription : Mai 2012
    Messages : 456
    Billets dans le blog
    17
    Par défaut
    oui ca marche mais regarde les sub d'evenement c'est fait pour ca, tu progressera sur le sujet, mais sinon c'est bien aussi ce que tu as fais

  5. #5
    Membre éclairé
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2015
    Messages
    56
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gers (Midi Pyrénées)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2015
    Messages : 56
    Par défaut
    Oui bien sûr je regarderai ça! Mais je vais d'abord finir le projet comme ça pour avoir une version fonctionnelle complète et puis je ferai une v2 en soignant le code et les détails plus tard. C'est mon premier projet sous vba donc je n'ai pas forcément les bons réflexes --"

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [XL-2010] Actualisation automatique avec une table access
    Par kandun dans le forum Excel
    Réponses: 2
    Dernier message: 02/02/2014, 15h46
  2. [AC-2010] Sélection automatique dans une listbox
    Par nianiania dans le forum IHM
    Réponses: 4
    Dernier message: 10/04/2012, 19h44
  3. Actualisation automatique d'une feuille à une autre
    Par baifal dans le forum Macros et VBA Excel
    Réponses: 4
    Dernier message: 03/09/2008, 17h34
  4. actualiser automatiquement une page web.
    Par MAJIK_ENIS dans le forum Servlets/JSP
    Réponses: 6
    Dernier message: 24/05/2006, 17h55
  5. [MFC] Probleme d'actualisation d'une listbox
    Par Robleplongeur dans le forum MFC
    Réponses: 5
    Dernier message: 13/05/2004, 14h15

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo