Valider donnée userform

Bonjour a tous

J'ai cette macro que je viens de créer tout seul

Mais la je suis perdu
je voudrais que lorsque je valide avec le "CommandButton3_Click()" la saisie du textbox2 ce fasse sur la meme ligne que mon choix de Combobox1 et combobox2 .

Option Explicit

Dim Ws As Worksheet
Dim NbLignes As Integer

Private Sub AJOUTER_Click()
If ComboBox1.Value = "" Then
MsgBox "il faut un SITE"
End If
If ComboBox2.Value = "" Then
MsgBox "il faut une ref"
Exit Sub
End If
Dim ligne As Long
ligne = Sheets("Feuil1").Range("A65536").End(xlUp).Row + 1
Sheets("Feuil1").Range("A" & ligne).Value = ComboBox1
Sheets("Feuil1").Range("B" & ligne).Value = ComboBox2
Sheets("Feuil1").Range("C" & ligne).Value = TextBox1
Unload Me
MsgBox " photo ajouté"
Call TRI

End Sub

Private Sub ComboBox1_Change()
Dim J As Long

  Me.ComboBox2.Clear
  If Me.ComboBox1.ListIndex = -1 Then Exit Sub
  With Me.ComboBox2
    For J = 2 To NbLignes
      If Ws.Range("A" & J) = Me.ComboBox1 Then
        .AddItem Ws.Range("B" & J)
        .List(.ListCount - 1, 1) = J
      End If
    Next J
  End With
End Sub

Private Sub ComboBox2_Change()
Dim ligne As Long
Dim I As Integer

  If Me.ComboBox2.ListIndex = -1 Then Exit Sub
  ligne = Me.ComboBox2.List(Me.ComboBox2.ListIndex, 1)
  For I = 1 To 2
    Me.Controls("TextBox" & I) = Ws.Cells(ligne, I + 2)
  Next I
End Sub

Private Sub CommandButton1_Click()
    Unload Me
Call TRI
End Sub

Private Sub CommandButton2_Click() 'ajouter image
Dim FICHIER As Variant
FICHIER = Application.GetOpenFilename("tout fichier (*jpg), *jpg")
If FICHIER = False Then Exit Sub
TextBox1 = FICHIER

Image.Picture = LoadPicture(TextBox1)
Image.PictureSizeMode = fmPictureSizeModeStretch
UserForm10.AJOUTER.Visible = True
End Sub

Private Sub CommandButton3_Click()

End Sub

Private Sub Image_Click()
MsgBox "non ça sert a rien de cliquer "
End Sub

Private Sub TextBox1_Change()
Image.Picture = LoadPicture(TextBox1)

End Sub

Private Sub TextBox2_Change()
UserForm10.CommandButton3.Visible = True

End Sub

Private Sub UserForm_Initialize()
    Set Ws = Worksheets("Feuil1")
    NbLignes = Ws.Range("A65536").End(xlUp).Row

    With Me.ComboBox2
      .ColumnCount = 2
      .ColumnWidths = "-1;0"
    End With

   InitCombo1
   UserForm10.AJOUTER.Visible = False
   UserForm10.CommandButton3.Visible = False
End Sub

Sub InitCombo1()
Dim J As Long
Dim Mondico As Object

  Set Mondico = CreateObject("Scripting.dictionary")
  For J = 2 To NbLignes
    Mondico(Ws.Range("A" & J).Value) = ""
  Next J
  With Me.ComboBox1
    .Clear
    If Mondico.Count > 0 Then
      .List = Application.Transpose(Mondico.keys)
    End If
  End With
End Sub

Merci de votre aide

Bonjour reinruof77

SVP

Bonjour BrunoM45

Désoler mon fichier est trop lourd.

voici un fichier test avec l'userform concerner.

5test.xlsm (16.92 Ko)

voici

Re, merci,

Bon je pense qu'il faut,
définir ta variable "Ligne" tout en haut de l'USF pour qu'elle puisse être utilisée dans chaque Sub

Option Explicit

Dim Ws As Worksheet
Dim NbLignes As Integer
Dim Ligne As Long

Ensuite

Private Sub CommandButton3_Click()
  With Sheets("Feuil1")
    .Range("D" & Ligne).Value = Me.TextBox2.Value
  End With
End Sub

A tester et nous dire

A+

Bonjour,

A tester :

Private Sub CommandButton3_Click()
    Dim ligne As Long
    Dim I As Integer

    For I = 2 To NbLignes
        If Ws.Cells(I, 1) = ComboBox1 And Ws.Cells(I, 2) = ComboBox2 Then
            ligne = I
            Exit For
        End If
    Next I

    If ligne > 0 Then
        Ws.Cells(ligne, 3) = TextBox2.Value
        MsgBox "La saisie a été faite sur la ligne " & ligne
    Else
        MsgBox "La combinaison de ComboBox1 et ComboBox2 n'a pas été trouvée"
    End If
End Sub

Ce code parcourt toutes les lignes de la feuille de calcul et vérifie si les valeurs des colonnes A et B correspondent respectivement aux valeurs de ComboBox1 et ComboBox2. Si oui, il stocke le numéro de la ligne dans la variable ligne. Il ajoute ensuite la valeur de TextBox2 à la colonne C de cette ligne. Si aucune correspondance n'est trouvée, il affiche un message d'erreur.

BrunoM45

Erreur 1004

1004

sur cette ligne

1005

Abderrahmane BENALI

Ton code fonctionne j'ai juste corriger cette ligne

Ws.Cells(ligne, 3) = TextBox2.Value

en

Ws.Cells(ligne, 4) = TextBox2.Value

Merci a tous les deux

Rechercher des sujets similaires à "valider donnee userform"