Problème VBA EXCEL

Résolu
Bonjour,

j'ai deux Textbox successifs qui indiquent les bornes Min(Textbox1) et Max(Textbox2).
je veux que lorsque la borne Min est vide ou lorsque par erreur la borne Min est sup au borne Max il y'aura un message box qui s'affiche (Borne Min> Borne Max) et le setfocus se positionne de nouveau sur la borne Min (Textbox1).

Merci de m'indiquer ou mettre le code (dans le before ou afterupdate)

Merci pour votre aide.

2 réponses

  1. Modérateur
    Bonjour,

    Un début de réponse avec l'événement _Exit des textbox :

    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If Not IsNumeric(TextBox1) Then
        MsgBox "Valeur non numérique"
        TextBox1 = ""
        Cancel = True
        Exit Sub
    End If
    If TextBox2 = "" Then Exit Sub
        
    If Val(TextBox1) > Val(TextBox2) Then
        MsgBox "Borne Min > Borne Max"
        TextBox1 = ""
        Cancel = True
    End If
    End Sub
    
    Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If Not IsNumeric(TextBox2) Then
        MsgBox "Valeur non numérique"
        TextBox2 = ""
        Cancel = True
        Exit Sub
    End If
    If Val(TextBox1) > Val(TextBox2) Then
        MsgBox "Borne Min > Borne Max"
        TextBox1.SetFocus
    End If
    End Sub

    0
    1. re Bonjour,

      Merci beaucoup pour votre aide, ca marche très bien et désolé pour le retard.

      à trés bientôt.
      0
  2. Modérateur
    je veux lorsque le textbox1 est vide et le textbox 2 non vide le EXIT sur le textbox2 me renvoie au textbox1 pour remplissage. j'ai essayé le code suivant mais ca marche pas:

    Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If TextBox1 = "" Then
    MsgBox " Borne Min vide !"
    Cancel = True
    End If
    End sub


    Le paramètre Cancel sert à autoriser (ou interdire) la sortie du Textbox. En mettant Cancel = True dans l'événement Exit du TextBox2 tu empêche la sortie...

    Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If TextBox1 = "" Then
       MsgBox " Borne Min vide !"
       TextBox1.SetFocus
    End If
    End sub


    0
    1. j'ai essayé mais le set focus n'a pas marché il se jette directement sur le textbox 3 !!

      voici mon code complet:

      Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
      
      If TextBox2 Mod 500 <> 0 Then
      TextBox2 = ""
      Cancel = True
      End If
      
      If Val(TextBox1) > Val(TextBox2) Then
      MsgBox "Borne Min > Borne Max"
      TextBox17.SetFocus
      End If
      
       If TextBox1 = "" Then
       MsgBox " Borne Min vide !"
       TextBox1.SetFocus
       End If
      
      End Sub
      
      
      Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
      
      If TextBox1 = "" Then
      Cancel = False
      Exit Sub
      End If
      
      If TextBox1 Mod 500 <> 0 Then
      TextBox1 = ""
      Cancel = True
      End If
      
      If TextBox2 = "" Then
      Exit Sub
      End If
      
      If Val(TextBox1) > Val(TextBox2) Then
      MsgBox "Borne Min > Borne Max"
      TextBox1 = ""
      Cancel = True
      End If
      
      End Sub
      0
    2. Modérateur
      @LANGAZOUBonjour,

      1- pour poster du code ici, place le entre les balises codes prévues à cet effet. Mode d'emploi.

      2- Tu m'étonnes que le SetFocus ne fonctionne pas... Tu attribues le Focus au TextBox17... Relis toi!

      3- teste ce code :
      Dim DejaPasse As Boolean 'cette ligne doit être en entête de ton Module, pas en plein milieu!!!
      
      Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
      If DejaPasse Then Exit Sub
      DejaPasse = False
      
      If TextBox2 = "" Then
          MsgBox "Borne max vide !"
          Cancel = True
      End If
      
      If TextBox1 = "" Then
          MsgBox "Borne Min vide !"
          DejaPasse = True
          TextBox1.SetFocus
      End If
      
      If Val(TextBox1) > Val(TextBox2) Then
          MsgBox "Borne Min > Borne Max !"
          DejaPasse = True
          TextBox1 = ""
          TextBox1.SetFocus
      End If
      
      If TextBox2 Mod 500 <> 0 Then
          TextBox2 = ""
          Cancel = True
      End If
      End Sub
      
      
      Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
      
      If TextBox1 = "" Then
          Cancel = True
          Exit Sub
      End If
      
      If TextBox1 Mod 500 <> 0 Then
          TextBox1 = ""
          Cancel = True
      End If
      
      If TextBox2 = "" Then
          Exit Sub
      End If
      
      If Val(TextBox1) > Val(TextBox2) Then
          MsgBox "Borne Min > Borne Max"
          TextBox1 = ""
          Cancel = True
      End If
      End Sub
      0
    3. C'est bon j'ai repéré l'erreur.

      Merci pour votre aide.
      0