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 :

Userform avec conditions


Sujet :

Macros et VBA Excel

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2014
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Industrie

    Informations forums :
    Inscription : Mai 2014
    Messages : 6
    Points : 5
    Points
    5
    Par défaut Userform avec conditions
    Bonjour à tous,

    Je rencontre quelques difficultés dans la construction d'un Userform. Il y a des champs que je souhaiterais rendre obligatoires, des champs dans lesquels je souhaiterais que les formats soient respectés (ex dates "jj/mm/yyyy" ou heures "hh:mm" ou n° de produit "pdt/_ _ _ / _ _ _ ") et je manque un peu de connaissances pour mener mon projet à bout...

    Même si je ne suis pas un expert en VBA en temps normal je m'efforce à chercher des lignes sur internets que j'adapte ensuite jusqu'à ce que ça marche cependant cette fois je n'arrive pas à combiner toutes les conditions/spécificités que je voudrais inclure

    Je vous mets le fichier en pièce jointe ce sera plus parlant, même si je suis certain qu'il y a pas mal de choses qui doivent vous sauter aux yeux en termes de simplification de mon code ci-dessous

    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
    Private Sub UserForm_Initialize()
     
    'Indications sur le format de remplissage lors de l'ouverture du formulaire
     
    With NewQueryUserForm.TextBox9
        .Text = "dd/mm/yyyy"
        .ForeColor = RGB(160, 160, 160)
        .Font.Size = 10
    End With
    With NewQueryUserForm.TextBox3
        .Text = "dd/mm/yyyy"
        .ForeColor = RGB(160, 160, 160)
        .Font.Size = 10
    End With
    With NewQueryUserForm.TextBox8
        .Text = "dd/mm/yyyy"
        .ForeColor = RGB(160, 160, 160)
        .Font.Size = 10
    End With
    With NewQueryUserForm.TextBox2
        .Text = "dd/mm/yyyy"
        .ForeColor = RGB(160, 160, 160)
        .Font.Size = 10
    End With
    With NewQueryUserForm.TextBox6
        .Text = "dd/mm/yyyy"
        .ForeColor = RGB(160, 160, 160)
        .Font.Size = 10
    End With
    With NewQueryUserForm.TextBox10
        .Text = "pdt/_ _ _ / _ _ _"
        .ForeColor = RGB(160, 160, 160)
        .Font.Size = 10
    End With
    With NewQueryUserForm.TextBox8
        .Text = "dd/mm/yyyy"
        .ForeColor = RGB(160, 160, 160)
        .Font.Size = 10
    End With
    With NewQueryUserForm.TextBox11
        .Text = "hh:mm"
        .ForeColor = RGB(160, 160, 160)
        .Font.Size = 10
    End With
    End Sub
     
     
    'Bouton CREATE dans formulaire
     
    Private Sub CommandButton1_Click()
    Dim Ligne As Long
    Ligne = ActiveSheet.Range("A65536").End(xlUp).Row + 1
     
     
    'Vérification des autres champs obligatoires : textbox avec dates 9 & 6, textbox 4& 5& 7, ComboBox 1& 2& 3
     
    If TextBox9 = "" Or TextBox6 = "" Or TextBox4 = "" Or TextBox5 = "" Or TextBox7 = "" Or ComboBox1.ListIndex < 0 Or ComboBox2.ListIndex < 0 Or ComboBox3.ListIndex < 0 Then
    MsgBox "Please fill all the mandatory fields"
     
    'vérification format dates pour les champs obligatoires 9 et 6
     
    If Not IsDate(NewQueryUserForm.TextBox9.Text) Then
    MsgBox "The Data entered is not a Date" _
    & Chr(10) & "Please Try again with the format dd/mm/yyyy"
     
    ElseIf IsDate(NewQueryUserForm.TextBox6.Text) Then
    MsgBox "The Data entered is not a Date" _
    & Chr(10) & "Please Try again with the format dd/mm/yyyy"
     
    'Ligne manquante: Si OptionButton 1 ou 2 coché alors:
     
    'ligne manquante: vérifier que TextBox11 est au format "hh:mm"
     
    'ligne manquante: vérifier que TextBox10 est au format "pdt/_ _ _ / _ _ _"
     
    If Not IsDate(NewQueryUserForm.TextBox8.Text) Then
    MsgBox "The Data entered is not a Date" _
    & Chr(10) & "Please Try again with the format dd/mm/yyyy"
     
    If Not IsDate(NewQueryUserForm.TextBox2.Text) Then
    MsgBox "The Data entered is not a Date" _
    & Chr(10) & "Please Try again with the format dd/mm/yyyy"
     
     
    'Créer la nouvelle ligne d'enregistrement
     
                                        ElseIf Cells(Ligne, 1) <> "" Then
                                               With ThisWorkbook.Sheets("Query Log")
                                                    ComboBox1 = .Cells(Ligne, 3)
                                                    ComboBox2 = .Cells(Ligne, 6)
                                                    ComboBox3 = .Cells(Ligne, 15)
                                                    TextBox1 = .Cells(Ligne, 11)
                                                    TextBox3 = .Cells(Ligne, 11)
                                                    TextBox10 = .Cells(Ligne, 7)
                                                    TextBox8 = .Cells(Ligne, 9)
                                                    TextBox11 = .Cells(Ligne, 10)
                                                    TextBox4 = .Cells(Ligne, 17)
                                                    TextBox5 = .Cells(Ligne, 18)
                                                    TextBox6 = .Cells(Ligne, 20)
                                                    TextBox7 = .Cells(Ligne, 19)
                                                End With
     
                                                    'Product serviceable Y/N ? Case à cocher
     
                                                        If OptionButton3.Value = True Then
                                                        Worksheet("Query Log").Cells(newrow, 13).Value = "Serviceable"
                                                        End If
                                                        If OptionButton2.Value = True Then
                                                        Worksheet.Cells(newrow, 13).Value = "Unserviceable"
                                                        End If
     
    'fermer après enregistremet du formulaire
    NewQueryUserForm.Hide
     
    End If
     
    End Sub
     
    'Bouton CANCEL  NewQueryUserForm
     
    Private Sub CommandButton2_Click()
    If MsgBox("Do you want to cancel the new query ?", vbYesNo, "Exit confirmation") = vbYes Then
    NewQueryUserForm.Hide
    End If
    End Sub
    Je vous remercie par avance de votre aide,

    Adrien
    Fichiers attachés Fichiers attachés

Discussions similaires

  1. Alimenter base de données à partir d'un userform avec des conditions
    Par VORT3X03 dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 25/06/2016, 08h47
  2. [XL-2000] Retranscription Userform avec conditions
    Par vbarth dans le forum Macros et VBA Excel
    Réponses: 0
    Dernier message: 02/04/2014, 17h36
  3. [XL-2010] Afficher des données dans un UserForm avec condition
    Par CmzxNene dans le forum Macros et VBA Excel
    Réponses: 12
    Dernier message: 17/01/2013, 02h42
  4. Réponses: 3
    Dernier message: 31/08/2006, 16h51
  5. Decode pour un if avec or dans la condition
    Par nkongolo.m dans le forum Oracle
    Réponses: 23
    Dernier message: 21/03/2006, 08h51

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