Bouton_Modifier_selectionner_listBox

Résolu
zouzou -  
yg_be Messages postés 23437 Date d'inscription   Statut Contributeur Dernière intervention   -

Bonjour,

Je voudrais modifier une donnée dans un tableau excel à partir d'une listBox ,je m'explique, 

quand je selectionne une donnée sur la listBox, les textbox se remplissent , je modifie une textbox et je clique sur le bouton modifier ( pour modifier ma donnée) mais ça se modifie pas, ( c'est la premiere donnée du tableau qui se modifie) ) je voudrais modifier la donnée selectionnée sur la listBox !

pourriez vous m'aider à ecrire le code !

ci joint mon code actuel

Merci infiniment

Private Sub b_modif_Click()

 Dim ligne As Integer
 
   If MsgBox("Confirmez-vous la modification ?", vbYesNo, "Confirmation de modification") <> vbYes Then

ligne = ListBox7.ListIndex + 2

MsgBox ligne

Range("A" & ligne) = T_Id.Value
Range("B" & ligne) = T_Designation.Value
Range("C" & ligne) = T_Code.Value
Range("D" & ligne) = T_Stock_Min.Value
Range("E" & ligne) = T_Nvx_Emplt.Value
Range("F" & ligne) = T_Fournisseur.Value
Range("G" & ligne) = T_Stock_Reel.Value
Range("H" & ligne) = T_Stock_Max.Value
Range("I" & ligne) = T_Ancien_Emplt.Value
Range("J" & ligne) = T_Machine.Value
Range("K" & ligne) = ComboBox1.Value
Range("L" & ligne) = ComboBox2.Value
Range("M" & ligne) = ComboBox3.Value
Range("N" & ligne) = ComboBox4.Value
Range("O" & ligne) = ComboBox5.Value
 
End If

End Sub


Windows / Firefox 102.0

11 réponses

  1. yg_be Messages postés 23437 Date d'inscription   Statut Contributeur Dernière intervention   Ambassadeur 1 588
     

    bonjour,

    peux-tu également montrer le code qui réagit quand tu selectionnes une donnée sur la listBox?

    0
  2. zouzou
     

    Bonjour yg_be

    voici le code quand je clique sur la listbox

    merci beaucoup pour tes aides

    Private Sub ListBox7_Click()
    
      Me.T_Id = Me.ListBox7.List(Me.ListBox7.ListIndex)
      Me.T_Designation.Value = Me.ListBox7.Column(1, Me.ListBox7.ListIndex)
      Me.T_Code.Value = Me.ListBox7.Column(2, Me.ListBox7.ListIndex)
      Me.T_Stock_Min.Value = Me.ListBox7.Column(3, Me.ListBox7.ListIndex)
      Me.T_Nvx_Emplt.Value = Me.ListBox7.Column(4, Me.ListBox7.ListIndex)
      Me.T_Fournisseur.Value = Me.ListBox7.Column(5, Me.ListBox7.ListIndex)
      Me.T_Stock_Reel.Value = Me.ListBox7.Column(6, Me.ListBox7.ListIndex)
      Me.T_Stock_Max.Value = Me.ListBox7.Column(7, Me.ListBox7.ListIndex)
      Me.T_Ancien_Emplt.Value = Me.ListBox7.Column(8, Me.ListBox7.ListIndex)
      Me.T_Machine.Value = Me.ListBox7.Column(9, Me.ListBox7.ListIndex)
      Me.ComboBox1.Value = Me.ListBox7.Column(10, Me.ListBox7.ListIndex)
      Me.ComboBox2.Value = Me.ListBox7.Column(11, Me.ListBox7.ListIndex)
      Me.ComboBox3.Value = Me.ListBox7.Column(12, Me.ListBox7.ListIndex)
      Me.ComboBox4.Value = Me.ListBox7.Column(13, Me.ListBox7.ListIndex)
      Me.ComboBox5.Value = Me.ListBox7.Column(14, Me.ListBox7.ListIndex)
    End Sub
    0
    1. yg_be Messages postés 23437 Date d'inscription   Statut Contributeur Dernière intervention   1 588
       

      bonjour,

      Je ne comprends pas pourquoi "ListBox7.ListIndex" ne contient plus la bonne valeur dans "b_modif_Click()".
      Moi, j'ajouterais un contrôle, pour mémoriser sa valeur.  Supposons que ce contrôle ajouté s'appelle ListX.

      Alors, dans "ListBox7_Click()", j'ajouterais:

      Me.ListX = Me.ListBox7.ListIndex

      Et dans "b_modif_Click", j'ajouterais:

      ligne=ListX
      0
  3. zouzou
     

    Bonjour yg_be,

    j'ai mis ligne = ListBox7.ListIndex + 2, parce que mes données commence à partir de la deuxieme ligne du tableau.

    je voudrais savoir s'il te plait la ListX que tu as ajouté est de quel type ? ( j'ai pas trop compris pourquoi tu as mis "Me.")

    quand je rajoute dans ListBox7_Click()",

    Me.ListX = Me.ListBox7.ListIndex

    Et dans "b_modif_Click", j'ajouterais:

    ligne=ListX

    ça m'affiche l'erreur au dessus

    Merci beaucoup d'avance

    0
    1. yg_be Messages postés 23437 Date d'inscription   Statut Contributeur Dernière intervention   1 588
       

      As-tu ajouté un contrôle nommé ListX?

      0
  4. zouzou
     
    • Private Sub b_modif_Click()
      
       Dim ligne As Integer
       
         If MsgBox("Confirmez-vous la modification ?", vbYesNo, "Confirmation de modification") <> vbYes Then
      ligne = ListX.Value
      'ligne = ListBox7.ListIndex + 2
      
      MsgBox ligne
      
      Range("A" & ligne) = Me.T_Id.Value
      Range("B" & ligne) = Me.T_Designation.Value
      Range("C" & ligne) = Me.T_Code.Value
      Range("D" & ligne) = Me.T_Stock_Min.Value
      Range("E" & ligne) = Me.T_Nvx_Emplt.Value
      Range("F" & ligne) = Me.T_Fournisseur.Value
      Range("G" & ligne) = Me.T_Stock_Reel.Value
      Range("H" & ligne) = Me.T_Stock_Max.Value
      Range("I" & ligne) = Me.T_Ancien_Emplt.Value
      Range("J" & ligne) = Me.T_Machine.Value
      Range("K" & ligne) = Me.ComboBox1.Value
      Range("L" & ligne) = Me.ComboBox2.Value
      Range("M" & ligne) = Me.ComboBox3.Value
      Range("N" & ligne) = Me.ComboBox4.Value
      Range("O" & ligne) = Me.ComboBox5.Value
       
      End If
      
      End Sub
    Private Sub ListBox7_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    If T_Fournisseur.Value = "" And T_Designation.Value = "" Then
    MsgBox "Introduisez un nom SVP!!"
    Exit Sub
    End If
    If T_Designation.Value <> "" Then
     Set cel = Feuil2.Columns(1).Find(what:=ListBox7.Value, LookIn:=xlValues, lookat:=xlWhole)
     If Not cel Is Nothing Then
     Me.SpinButton2 = cel.Row
     End If
     End If
    btncreer.Enabled = False
    End Sub
    
    Private Sub ListBox7_Click()
      Me.ListX = Me.ListBox7.ListIndex
      Me.T_Id = Me.ListBox7.List(Me.ListBox7.ListIndex)
      Me.T_Designation.Value = Me.ListBox7.Column(1, Me.ListBox7.ListIndex)
      Me.T_Code.Value = Me.ListBox7.Column(2, Me.ListBox7.ListIndex)
      Me.T_Stock_Min.Value = Me.ListBox7.Column(3, Me.ListBox7.ListIndex)
      Me.T_Nvx_Emplt.Value = Me.ListBox7.Column(4, Me.ListBox7.ListIndex)
      Me.T_Fournisseur.Value = Me.ListBox7.Column(5, Me.ListBox7.ListIndex)
      Me.T_Stock_Reel.Value = Me.ListBox7.Column(6, Me.ListBox7.ListIndex)
      Me.T_Stock_Max.Value = Me.ListBox7.Column(7, Me.ListBox7.ListIndex)
      Me.T_Ancien_Emplt.Value = Me.ListBox7.Column(8, Me.ListBox7.ListIndex)
      Me.T_Machine.Value = Me.ListBox7.Column(9, Me.ListBox7.ListIndex)
      Me.ComboBox1.Value = Me.ListBox7.Column(10, Me.ListBox7.ListIndex)
      Me.ComboBox2.Value = Me.ListBox7.Column(11, Me.ListBox7.ListIndex)
      Me.ComboBox3.Value = Me.ListBox7.Column(12, Me.ListBox7.ListIndex)
      Me.ComboBox4.Value = Me.ListBox7.Column(13, Me.ListBox7.ListIndex)
      Me.ComboBox5.Value = Me.ListBox7.Column(14, Me.ListBox7.ListIndex)
    End Sub

    Bonjour ,

    j'ai ajouté un textbox au userform, je l'ai nommée ListX,  ci joint mes deux codes du bouton modifier et du listbox. ainsi que la simulation.

    la listX qui entouré en bleu , elle affiche le numero de la ligne dans la listbox ( la ligne dans la listbox n'est pas la ligne sur le tableau excel !)  et dans donc je peux pas affecter la valeur de la listX à la variable ligne du code de mon bouton modifier !

    Merci beaucoup d'avance

    0
  5. Vous n’avez pas trouvé la réponse que vous recherchez ?

    Posez votre question
  6. zouzou
     

    Je pense je vais utiliser la meme astuce que tu m'as montré sur ( le bouton supprimé ) tester les Id si sont egeaux et puis modifier à la place de supprimer , ça me une  parait bonne astuce

    0
    1. zouzou
       
      Private Sub b_modif_Click()
      
       Dim ligne As Integer
       Dim i As Long, suppression As String, present As String
       
         If MsgBox("Confirmez-vous la modification ?", vbYesNo, "Confirmation de modification") <> vbYes Then
         With ThisWorkbook.Sheets("Electrique")
          For i = .Range("A" & .Rows.Count).End(xlUp).Row To 2 Step -1
           present = .Range("A" & i).Value
          If present = Me.T_Id.Value Then
          
      Range("A" & ligne) = Me.T_Id.Value
      Range("B" & ligne) = Me.T_Designation.Value
      Range("C" & ligne) = Me.T_Code.Value
      Range("D" & ligne) = Me.T_Stock_Min.Value
      Range("E" & ligne) = Me.T_Nvx_Emplt.Value
      Range("F" & ligne) = Me.T_Fournisseur.Value
      Range("G" & ligne) = Me.T_Stock_Reel.Value
      Range("H" & ligne) = Me.T_Stock_Max.Value
      Range("I" & ligne) = Me.T_Ancien_Emplt.Value
      Range("J" & ligne) = Me.T_Machine.Value
      Range("K" & ligne) = Me.ComboBox1.Value
      Range("L" & ligne) = Me.ComboBox2.Value
      Range("M" & ligne) = Me.ComboBox3.Value
      Range("N" & ligne) = Me.ComboBox4.Value
      Range("O" & ligne) = Me.ComboBox5.Value
      
      End If
      End With
       Next i
       End If
      End Sub

      je pense que ce code doit passer ! par contre je compile , àa m'affiche :

      end with sans with

      end if sans bloc if ...

      je comprend pas l'erreur

      merci beaucoup d'avance

      0
      1. yg_be Messages postés 23437 Date d'inscription   Statut Contributeur Dernière intervention   1 588 > zouzou
         

        Il faut d'abord clôturer le dernier element ouvert.
        Puisque le code commence par 

        •  If ,
        • With, 
        • For, 
        • If,

        il doit se terminer par 

        • end if
        • next
        • end with
        • end if

        Par ailleurs, tu utilises deux variables, i et ligne, alors qu'une seule est suffisante.

        0
  7. zouzou
     

    Merci beaucoup yg_be ,

    je partage avec toi mon code qui deverait marchait normalement, c'est la meme astuce que j'ai fait sur le bouton supprimé ! à ton avis pourquoi ça modifie pas la selection et ne montre aucune erreur ! le code est correct .

    je te remercie beaucoup d'avance

    Private Sub b_modif_Click()
    
    
     Dim i As Long, modification As String, present As String
     
       If MsgBox("Confirmez-vous la modification ?", vbYesNo, "Confirmation de modification") <> vbYes Then
       With ThisWorkbook.Sheets("Electrique")
        For i = .Range("A" & .Rows.Count).End(xlUp).Row To 2 Step -1
         present = .Range("A" & i).Value
         modification = Me.T_Id.Value
        If present = modification Then
        modification = modification + 1
        
    Range("A" & modification) = Me.T_Id.Value
    Range("B" & modification) = Me.T_Designation.Value
    Range("C" & modification) = Me.T_Code.Value
    Range("D" & modification) = Me.T_Stock_Min.Value
    Range("E" & modification) = Me.T_Nvx_Emplt.Value
    Range("F" & modification) = Me.T_Fournisseur.Value
    Range("G" & modification) = Me.T_Stock_Reel.Value
    Range("H" & modification) = Me.T_Stock_Max.Value
    Range("I" & modification) = Me.T_Ancien_Emplt.Value
    Range("J" & modification) = Me.T_Machine.Value
    Range("K" & modification) = Me.ComboBox1.Value
    Range("L" & modification) = Me.ComboBox2.Value
    Range("M" & modification) = Me.ComboBox3.Value
    Range("N" & modification) = Me.ComboBox4.Value
    Range("O" & modification) = Me.ComboBox5.Value
    
    End If
     Next i
     End With
     End If
    End Sub

    :

    0
    1. yg_be Messages postés 23437 Date d'inscription   Statut Contributeur Dernière intervention   1 588
       

      peut-être:

          For i = .Range("A" & .Rows.Count).End(xlUp).Row To 2 Step -1
              If .Range("A" & i).Value = Me.T_Id.Value Then
                  .Range("B" & i) = Me.T_Designation.Value
                  ' ...
      0
  8. zouzou
     

    Je te remercie pour la proposition , je l'ai testé , il est correcte mais il donne rien comme resultat !

    Private Sub b_modif_Click()
    
    
     Dim i As Long, modification As String, present As String
     
       If MsgBox("Confirmez-vous la modification ?", vbYesNo, "Confirmation de modification") <> vbYes Then
       With ThisWorkbook.Sheets("Electrique")
        For i = .Range("A" & .Rows.Count).End(xlUp).Row To 2 Step -1
         If .Range("A" & i).Value = Me.T_Id.Value Then
    .Range("B" & i) = Me.T_Designation.Value
    .Range("C" & i) = Me.T_Code.Value
    .Range("D" & i) = Me.T_Stock_Min.Value
    .Range("E" & i) = Me.T_Nvx_Emplt.Value
    .Range("F" & i) = Me.T_Fournisseur.Value
    .Range("G" & i) = Me.T_Stock_Reel.Value
    .Range("H" & i) = Me.T_Stock_Max.Value
    .Range("I" & i) = Me.T_Ancien_Emplt.Value
    .Range("J" & i) = Me.T_Machine.Value
    .Range("K" & i) = Me.ComboBox1.Value
    .Range("L" & i) = Me.ComboBox2.Value
    .Range("M" & i) = Me.ComboBox3.Value
    .Range("N" & i) = Me.ComboBox4.Value
    .Range("O" & i) = Me.ComboBox5.Value
       '  present = .Range("A" & i).Value
       '  modification = Me.T_Id.Value
       ' If present = modification Then
       ' modification = modification + 1
        
    'Range("A" & modification) = Me.T_Id.Value
    'Range("B" & modification) = Me.T_Designation.Value
    End If
     Next i
     End With
     End If
    End Sub
    0
    1. mzouali Messages postés 22 Statut Membre > yg_be Messages postés 23437 Date d'inscription   Statut Contributeur Dernière intervention  
       

      Bonjour yg_be,

      le msgBox aprés le if ne marche pas et avant le if marche

      Private Sub b_modif_Click()
      
       Dim i As Long, modif As String, present As String
          If MsgBox("Confirmez-vous la modification ?", vbYesNo, "Confirmation de modification") = vbYes Then
         
          
         With ThisWorkbook.Sheets("Electrique")
         For i = .Range("A" & .Rows.Count).End(xlUp).Row To 2 Step -1
          If .Range("A" & i).Value = Me.T_Id.Value Then
           MsgBox ("modification confirmée")
      0
    2. yg_be Messages postés 23437 Date d'inscription   Statut Contributeur Dernière intervention   1 588 > mzouali Messages postés 22 Statut Membre
       

      peut-être ajouter avant le premier "end if"

      else
          msgbox .Range("A" & i).Value & "|" & Me.T_Id.Value

      Cela devrait montrer les valeurs comparées, et confirmer qu'elles sont différentes.

      1
    3. mzouali Messages postés 22 Statut Membre > yg_be Messages postés 23437 Date d'inscription   Statut Contributeur Dernière intervention  
       

      oui effectivement, elles sont differentes

      voici le resultat en boucle 9|1 puis 8|1  7|1   6|1    5|1    4|1   3|1   2|1    1|1 :

      0
    4. yg_be Messages postés 23437 Date d'inscription   Statut Contributeur Dernière intervention   1 588 > mzouali Messages postés 22 Statut Membre
       

      Bizarre que 1|1 soit aussi différent...

      1
  9. ziz
     

    Bonjour,

    je reviens vers pour resoudre le meme problme du bouton modifier, j'avais un deplacement qui m'a enmeché d'avancer.

    resultat : quand les deux valeurs Range("A" & i).Value et Me.T_Id.Value sont egaux il continue à comparer sans modifier la valeur de la cellule !

    Private Sub b_modif_Click()
    
     Dim i As Long, modification As String, present As String
    
        
       With ThisWorkbook.Sheets("Electrique")
        For i = .Range("A" & .Rows.Count).End(xlUp).Row To 2 Step -1
            If Range("A" & i).Value = Me.T_Id.Value Then
      MsgBox .Range("A" & i).Value & "|" & Me.T_Id.Value
      Range("B" & i) = Me.T_Designation.Value
           Else
        MsgBox .Range("A" & i).Value & "|" & Me.T_Id.Value
        
    
    End If
     Next i
     End With
     'End If
    End Sub

    elle passe sur l'egalité et il continue à comprarer

    jusqu'à monté tout en haut

    avez une idée sur comment modifier un contenu de celulle

    je vous remercie d'avance

    0
    1. yg_be Messages postés 23437 Date d'inscription   Statut Contributeur Dernière intervention   1 588
       

      le plus intéressant, c'est que tu as réussi à faire deux msgbox qui ne te permettent pas de différencier les deux conditions.

      0
  10. ziz
     

    je te remercie pour tes remarques, j'ai garder que le 1er msgBox et j'ai rien ne s'affiche

       With ThisWorkbook.Sheets("Electrique")
        For i = .Range("A" & .Rows.Count).End(xlUp).Row To 2 Step -1
            If Range("A" & i).Value = Me.T_Id.Value Then
      MsgBox .Range("A" & i).Value & "|" & Me.T_Id.Value
      Range("B" & i) = Me.T_Designation.Value
           Else
        'MsgBox .Range("A" & i).Value & "|" & Me.T_Id.Value
    0
    1. yg_be Messages postés 23437 Date d'inscription   Statut Contributeur Dernière intervention   1 588
       

      essaie plutôt:

      If Range("A" & i).Value = Me.T_Id.Value Then
        MsgBox   .Range("A" & i).Value & " = " & Me.T_Id.Value
        Range("B" & i) = Me.T_Designation.Value
      Else
        MsgBox   .Range("A" & i).Value & " <> " & Me.T_Id.Value
      0
  11. ziz
     

    ça donne le meme resultat :

    il continue à comparer jusqu'à comprarer tous le tableau ...

    0
    1. yg_be Messages postés 23437 Date d'inscription   Statut Contributeur Dernière intervention   1 588
       

      Je pense que la solution est en #21.

      0
  12. ziz
     

    Bonjour yg_be

    vous voulez dire quoi par #21 ?

    enfin j'ai reussi à modifier le tableau en passant par un bouton modifier :

    voici le code :

       
    With ThisWorkbook.Sheets("Electrique")  ' je definie la feuille où je travail 
        For i = .Range("A" & .Rows.Count).End(xlUp).Row To 2 Step -1  'une boucle qui traite toute les lignes de mon tableau.
        If Cells(i, 1) = Me.T_Id.Text Then    'comparaison de toute les lignes de ma premiere colonne  avec la valeur du textbox nommé " T_Id", si ils sont egaux alors il selectionne la cellule qui à coté du droite et il modifie la valeur de cette cellule par la valeur du T_Designation " textbox dont le userform" . 
    
        Cells(i, 1).Select
        Exit For
        End If
        Next i
        ActiveCell.Offset(0, 1) = Me.T_Designation.Value    ' modification 

    Je te remercie infiniment yr_be

    0
    1. yg_be Messages postés 23437 Date d'inscription   Statut Contributeur Dernière intervention   1 588
       

      #21, c'est la réponse faite le 27 juil. 2022 à 21:18.

      0