XL 2016 Mise à jour dans un tableau structuré

  • Initiateur de la discussion Initiateur de la discussion halecs93
  • Date de début Date de début

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

halecs93

XLDnaute Impliqué
Bonjour à tout le monde

Mon userform me permet d'écrire un titre, de définir un style (ou genre) etc. et ensuite, de l'ajouter à ma liste.

Je cherche un moyen, si j'écris un titre déjà dans ma liste de modifier les infos et de mettre à jour cette liste.

Une idée ?

Merci

1744736293481.png
 

Pièces jointes

bonjour

au moment d'ajouter (AVANT d'ajouter)
1) tu cherches si il existe
2) si il existe: tu notes sa position (ligne=trouve.row-.range.row=
3) si il n'existe pas: ligne=.listrows.add.index
4) tu enregistres les données à ligne...
Finalement, j'ai usé du code
VB:
Private Sub CommandButton4_Click()
    Dim ws As Worksheet
    Dim tbl As ListObject
    Dim Ind As Long
    Dim i As Long
    Dim found As Boolean

    ' Vérifier la saisie
    If ComboBox1.ListIndex = -1 Then
        MsgBox "Veuillez sélectionner une option dans la liste déroulante.", vbExclamation
        Exit Sub
    End If

    If Trim(TextBox2.Text) = "" Then
        MsgBox "Veuillez remplir la description dans TextBox2.", vbExclamation
        Exit Sub
    End If

    Application.EnableEvents = False
    Set ws = ActiveSheet
    Set tbl = ws.ListObjects(1)
    found = False

    ' Chercher si la valeur de TextBox2 existe déjà dans la colonne A
    With tbl
        For i = 1 To .ListRows.Count
            If .DataBodyRange(i, 1).Value = TextBox2.Text Then
                ' Mise à jour des autres colonnes
                .DataBodyRange(i, 2) = TextBox1.Text
                .DataBodyRange(i, 3) = ComboBox1.Value
                .DataBodyRange(i, 4) = Chr(168)
                found = True
                Exit For
            End If
        Next i

        ' Si non trouvé, on ajoute une nouvelle ligne
        If Not found Then
            Ind = .ListRows.Add.Index
            .DataBodyRange(Ind, 1) = TextBox2.Text
            .DataBodyRange(Ind, 2) = TextBox1.Text
            .DataBodyRange(Ind, 3) = ComboBox1.Value
            .DataBodyRange(Ind, 4) = Chr(168)
        End If

        ' Trier le tableau sur la colonne A
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=.DataBodyRange.Columns(1), SortOn:=xlSortOnValues, _
            Order:=xlAscending, DataOption:=xlSortNormal
        With .Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End With

    ' Message de confirmation
    If found Then
        MsgBox "Ligne mise à jour avec succès.", vbInformation
    Else
        MsgBox "Nouvelle ligne ajoutée avec succès.", vbInformation
    End If

    ' Réinitialisation des champs
    TextBox2.Text = ""
    ComboBox1.ListIndex = -1
    Application.EnableEvents = True
End Sub
 
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD

Discussions similaires

Réponses
24
Affichages
355
Réponses
8
Affichages
314
Retour