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 :

Boucle For dans Userform


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Janvier 2010
    Messages
    55
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Janvier 2010
    Messages : 55
    Par défaut Boucle For dans Userform
    Bonjour à tous,

    Pourriez-vous m'aider ? j'ai un souci dans mon code au niveau de la boucle.

    Si je supprime la condition Else en fin de boucle, le code fonctionne mais si la valeur de la combobox n'est pas présente en colonne 4 alors je reçois une erreur.

    Avez-vous une idée d'où proviendrait mon erreur ?

    Merci d'avance pour votre aide.

    Code VBA : 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
    Private Sub ComboBox13_Change()
    Dim date1 As Date, date2 As Date, Duree As Integer, dlng As Integer, i As Integer
    Dim rval As String
    Dim rpark As String
    dlng = Range("C65536").End(xlUp).Row
     
    For i = 27 To dlng
    If Me.ComboBox13 = "" Then Exit Sub
    date1 = Cells(i, 2).Value
    date2 = Date
    Duree = DateDiff("d", date1, date2)
    If Duree > 2 And Cells(i, 4).Value = Me.ComboBox13.Value Then
    Cells(i, 9).Value = Duree
    rval = Cells(i, 5).Value
    rpark = Cells(i, 4).Value
     
    Dim strEnvoyer As String
    strEnvoyer = "xxxxx@xxxx" ' Mailto: To
    Dim OutMail As Object
    Dim MonOutlook As Object
    Dim MonMessage As Object
    Dim strBody As String
    Dim strSignature As String
    Set MonOutlook = CreateObject("Outlook.Application")
    Set MonMessage = MonOutlook.CreateItem(0)
    MonMessage.To = strEnvoyer
    MonMessage.Subject = "Emergency exit not controlled " & Date
     
        strSignature = MonMessage.HTMLBody
        strBody = strBody & "<table style=width:100%; cellpadding=20; cellspacing=1; border=1>"
        strBody = strBody & "<tr>"
        strBody = strBody & "<th>Parking</th>"
        strBody = strBody & "<th>Emergency exit not controlled</th>"
        strBody = strBody & "<th>Delay</th>"
        strBody = strBody & "</tr>"
        strBody = strBody & "<tr>"
        strBody = strBody & "<td align=center valign=middle>" & Replace(rpark, vbCrLf, "</td>")
        strBody = strBody & "<td align=center valign=middle>" & Replace(rval, vbCrLf, "</td>")
        strBody = strBody & "<td align=center valign=middle>" & Replace(Duree, vbCrLf, "</td>")
        strBody = strBody & "</tr>"
        strBody = strBody & "</table>"
    Else
    If Me.ComboBox13.Value <> Cells(i, 4).Value Then
       GoTo demande1
    End If
    End If
     
    Next i
     
    strBody = strBody & "<p style font-family=courier; font-size=24; style=color:#090909>" & "<b>" & "Best regards</b></p>"
    strBody = strBody & "<p style font-family=courier; font-size=24; style=color:#090909>" & UCase(Environ("USERNAME"))
    MonMessage.HTMLBody = strBody
    ThisWorkbook.Save
    MonMessage.Attachments.Add ActiveWorkbook.Path & "\" & ActiveWorkbook.Name
    MonMessage.send
    Set MonOutlook = Nothing
     
    demande1:
    'Cherche la première ligne vide
    lig = Range("B" & Rows.Count).End(xlUp).Row + 1
    'Copie le parking choisi dans la colonne D
    Cells(lig, 4) = Me.ComboBox13
    If Me.ComboBox13 <> "" Then
        'Met la date et l'heure dans les colonne B et C
        Me.TextBox55 = Date: Cells(lig, 2) = Me.TextBox55
        Me.TextBox103 = Time: Cells(lig, 3) = Me.TextBox103
    Else
        Exit Sub
    End If
    'Description de la sortie de secours
    EMERGENCY_EXIT = InputBox("Please enter the name of the emergency exit ", "Emergency exit ?")
    Me.TextBox57 = EMERGENCY_EXIT: Cells(lig, 5) = EMERGENCY_EXIT
    If Me.TextBox57 <> "" Then
    Cells(lig, 6) = "True"
    Else
    Cells(lig, 6) = "False"
    End If
     
    'CHECKED BY
    CHECKED_BY = InputBox("Please enter the name of the person who made the control ?")
    Me.TextBox60 = CHECKED_BY: Cells(lig, 7) = CHECKED_BY
     
    'Commentaires
    Comments = InputBox("please enter your comments ?")
    Me.TextBox59 = Comments: Cells(lig, 8) = Comments
     
    End Sub

  2. #2
    Expert confirmé
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2013
    Messages
    3 609
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Alimentation

    Informations forums :
    Inscription : Mai 2013
    Messages : 3 609
    Par défaut
    Bonjour,

    De quel Else parles-tu ? et sur quelle ligne apparaît l'erreur ?
    PS: quand tu mets du code utilise le bouton # pour le formater et le rendre plus lisible...

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Janvier 2010
    Messages
    55
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Janvier 2010
    Messages : 55
    Par défaut
    Bonjour,

    Merci pour ton aide.

    Voici le message d'erreur. Si je supprime cette condition:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    Else
    If Me.ComboBox13.Value <> Cells(i, 4).Value Then
       GoTo demande1
    End If

    Nom : erreur.jpg
Affichages : 220
Taille : 464,4 Ko

    Par contre si la valeur de ma combobox est bien dans mon tableau en colonne 4 alors tout fonctionne et le mail est envoyé.

    Bien à toi,

    voici mon code :

    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
    Private Sub ComboBox13_Change()
    Dim date1 As Date, date2 As Date, Duree As Integer, dlng As Integer, i As Integer
    Dim rval As String
    Dim rpark As String
    dlng = Range("C65536").End(xlUp).Row
     
    For i = 27 To dlng
    If Me.ComboBox13 = "" Then Exit Sub
    date1 = Cells(i, 2).Value
    date2 = Date
    Duree = DateDiff("d", date1, date2)
    If Duree > 2 And Cells(i, 4).Value = Me.ComboBox13.Value Then
    Cells(i, 9).Value = Duree
    rval = Cells(i, 5).Value
    rpark = Cells(i, 4).Value
     
    Dim strEnvoyer As String
    strEnvoyer = "xxxxx@xxxx" ' Mailto: To
    Dim OutMail As Object
    Dim MonOutlook As Object
    Dim MonMessage As Object
    Dim strBody As String
    Dim strSignature As String
    Set MonOutlook = CreateObject("Outlook.Application")
    Set MonMessage = MonOutlook.CreateItem(0)
    MonMessage.To = strEnvoyer
    MonMessage.Subject = "Emergency exit not controlled " & Date
     
        strSignature = MonMessage.HTMLBody
        strBody = strBody & "<table style=width:100%; cellpadding=20; cellspacing=1; border=1>"
        strBody = strBody & "<tr>"
        strBody = strBody & "<th>Parking</th>"
        strBody = strBody & "<th>Emergency exit not controlled</th>"
        strBody = strBody & "<th>Delay</th>"
        strBody = strBody & "</tr>"
        strBody = strBody & "<tr>"
        strBody = strBody & "<td align=center valign=middle>" & Replace(rpark, vbCrLf, "</td>")
        strBody = strBody & "<td align=center valign=middle>" & Replace(rval, vbCrLf, "</td>")
        strBody = strBody & "<td align=center valign=middle>" & Replace(Duree, vbCrLf, "</td>")
        strBody = strBody & "</tr>"
        strBody = strBody & "</table>"
    Else
    If Me.ComboBox13.Value <> Cells(i, 4).Value Then
       GoTo demande1
    End If
    End If
     
    Next i
     
    strBody = strBody & "<p style font-family=courier; font-size=24; style=color:#090909>" & "<b>" & "Best regards</b></p>"
    strBody = strBody & "<p style font-family=courier; font-size=24; style=color:#090909>" & UCase(Environ("USERNAME"))
    MonMessage.HTMLBody = strBody
    ThisWorkbook.Save
    MonMessage.Attachments.Add ActiveWorkbook.Path & "\" & ActiveWorkbook.Name
    MonMessage.send
    Set MonOutlook = Nothing
     
    demande1:
    'Cherche la première ligne vide
    lig = Range("B" & Rows.Count).End(xlUp).Row + 1
    'Copie le parking choisi dans la colonne D
    Cells(lig, 4) = Me.ComboBox13
    If Me.ComboBox13 <> "" Then
        'Met la date et l'heure dans les colonne B et C
        Me.TextBox55 = Date: Cells(lig, 2) = Me.TextBox55
        Me.TextBox103 = Time: Cells(lig, 3) = Me.TextBox103
    Else
        Exit Sub
    End If
    'Description de la sortie de secours
    EMERGENCY_EXIT = InputBox("Please enter the name of the emergency exit ", "Emergency exit ?")
    Me.TextBox57 = EMERGENCY_EXIT: Cells(lig, 5) = EMERGENCY_EXIT
    If Me.TextBox57 <> "" Then
    Cells(lig, 6) = "True"
    Else
    Cells(lig, 6) = "False"
    End If
     
    'CHECKED BY
    CHECKED_BY = InputBox("Please enter the name of the person who made the control ?")
    Me.TextBox60 = CHECKED_BY: Cells(lig, 7) = CHECKED_BY
     
    'Commentaires
    Comments = InputBox("please enter your comments ?")
    Me.TextBox59 = Comments: Cells(lig, 8) = Comments
     
    End Sub

  4. #4
    Expert confirmé
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2013
    Messages
    3 609
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Alimentation

    Informations forums :
    Inscription : Mai 2013
    Messages : 3 609
    Par défaut
    OK, tu initialises tom Message à l'intérieur de ta boucle si la condition est vraie
    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
    If Duree > 2 And Cells(i, 4).Value = Me.ComboBox13.Value Then
    Cells(i, 9).Value = Duree
    rval = Cells(i, 5).Value
    rpark = Cells(i, 4).Value
     
    Dim strEnvoyer As String
    strEnvoyer = "xxxxx@xxxx" ' Mailto: To
    Dim OutMail As Object
    Dim MonOutlook As Object
    Dim MonMessage As Object
    Dim strBody As String
    Dim strSignature As String
    Set MonOutlook = CreateObject("Outlook.Application")
    Set MonMessage = MonOutlook.CreateItem(0)
    '................
    Si la condition est fausse, MonMessage et MonOutlook ne sont pas initialisés et ça cause une erreur.
    Tu pourrais initialiser à l'extérieur de la condition

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    For i = 27 To dlng
       If Me.ComboBox13 = "" Then Exit Sub
          date1 = Cells(i, 2).Value
          date2 = Date
          Duree = DateDiff("d", date1, date2)
          Set MonOutlook = CreateObject("Outlook.Application")
          Set MonMessage = MonOutlook.CreateItem(0)
       If Duree > 2 And Cells(i, 4).Value = Me.ComboBox13.Value Then
    '.....
    Et habitue-toi à déclarer toutes les variables en entête de procédure

  5. #5
    Membre averti
    Profil pro
    Inscrit en
    Janvier 2010
    Messages
    55
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Janvier 2010
    Messages : 55
    Par défaut
    Bonsoir,

    Je pensais que c'était réglé mais maintenant je n'ai plus le mail qui est envoyé.

    Ci-dessous le code adapté :

    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
    Private Sub ComboBox13_Change()
    Dim date1 As Date, date2 As Date, Duree As Integer, dlng As Integer, i As Integer
    Dim rval As String
    Dim rpark As String
    Dim strEnvoyer As String
    Dim OutMail As Object
    Dim MonOutlook As Object
    Dim MonMessage As Object
    Dim strBody As String
    Dim strSignature As String
     
    dlng = Range("C65536").End(xlUp).Row
     
    For i = 27 To dlng
    If Me.ComboBox13 = "" Then Exit Sub
    date1 = Cells(i, 2).Value
    date2 = Date
    Duree = DateDiff("d", date1, date2)
    Set MonOutlook = CreateObject("Outlook.Application")
    Set MonMessage = MonOutlook.CreateItem(0)
    If Duree > 2 And Cells(i, 4).Value = Me.ComboBox13.Value Then
    Cells(i, 9).Value = Duree
    rval = Cells(i, 5).Value
    rpark = Cells(i, 4).Value
     
     
    strEnvoyer = "xxxxxx" ' Mailto: To
    MonMessage.To = strEnvoyer
    MonMessage.Subject = "Emergency exit not controlled " & Date
     
        strSignature = MonMessage.HTMLBody
        strBody = strBody & "<table style=width:100%; cellpadding=20; cellspacing=1; border=1>"
        strBody = strBody & "<tr>"
        strBody = strBody & "<th>Parking</th>"
        strBody = strBody & "<th>Emergency exit not controlled</th>"
        strBody = strBody & "<th>Delay</th>"
        strBody = strBody & "</tr>"
        strBody = strBody & "<tr>"
        strBody = strBody & "<td align=center valign=middle>" & Replace(rpark, vbCrLf, "</td>")
        strBody = strBody & "<td align=center valign=middle>" & Replace(rval, vbCrLf, "</td>")
        strBody = strBody & "<td align=center valign=middle>" & Replace(Duree, vbCrLf, "</td>")
        strBody = strBody & "</tr>"
        strBody = strBody & "</table>"
    Else
    If Me.ComboBox13.Value <> Cells(i, 4).Value Then
       GoTo demande1
    End If
    End If
     
    Next i
     
    strBody = strBody & "<p style font-family=courier; font-size=24; style=color:#090909>" & "<b>" & "Best regards</b></p>"
    strBody = strBody & "<p style font-family=courier; font-size=24; style=color:#090909>" & UCase(Environ("USERNAME"))
    MonMessage.HTMLBody = strBody
    ThisWorkbook.Save
    MonMessage.Attachments.Add ActiveWorkbook.Path & "\" & ActiveWorkbook.Name
    MonMessage.send
    Set MonOutlook = Nothing
     
    demande1:
    'Cherche la première ligne vide
    lig = Range("B" & Rows.Count).End(xlUp).Row + 1
    'Copie le parking choisi dans la colonne D
    Cells(lig, 4) = Me.ComboBox13
    If Me.ComboBox13 <> "" Then
        'Met la date et l'heure dans les colonne B et C
        Me.TextBox55 = Date: Cells(lig, 2) = Me.TextBox55
        Me.TextBox103 = Time: Cells(lig, 3) = Me.TextBox103
    Else
        Exit Sub
    End If
    'Description de la sortie de secours
    EMERGENCY_EXIT = InputBox("Please enter the name of the emergency exit ", "Emergency exit ?")
    Me.TextBox57 = EMERGENCY_EXIT: Cells(lig, 5) = EMERGENCY_EXIT
    If Me.TextBox57 <> "" Then
    Cells(lig, 6) = "True"
    Else
    Cells(lig, 6) = "False"
    End If
     
    'CHECKED BY
    CHECKED_BY = InputBox("Please enter the name of the person who made the control ?")
    Me.TextBox60 = CHECKED_BY: Cells(lig, 7) = CHECKED_BY
     
    'Commentaires
    Comments = InputBox("please enter your comments ?")
    Me.TextBox59 = Comments: Cells(lig, 8) = Comments
     
    End Sub
    petite précision code a bien été testé avec une adresse mail valide

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    strEnvoyer = "xxxxxx@xxxxx" ' Mailto: To

  6. #6
    Expert confirmé
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2013
    Messages
    3 609
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Alimentation

    Informations forums :
    Inscription : Mai 2013
    Messages : 3 609
    Par défaut
    Si ça ne fonctionne pas, passe en pas à pas (F8) et regarde la valeur de tes différentes variables pour comprendre ce qui ne va pas.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. boucle "for" dans "do while"
    Par oranoutan dans le forum C#
    Réponses: 13
    Dernier message: 05/06/2007, 22h13
  2. Une boucle for dans un switch case?
    Par Death83 dans le forum Langage
    Réponses: 4
    Dernier message: 26/04/2006, 15h05
  3. Boucle For dans un TDBGrid
    Par Latipolia dans le forum C++Builder
    Réponses: 28
    Dernier message: 14/06/2005, 13h12
  4. boucle for dans un if...
    Par SylverFox dans le forum Débuter
    Réponses: 3
    Dernier message: 11/08/2004, 17h57
  5. Boucle for dans un script cmd
    Par nicolas.ganache dans le forum Développement
    Réponses: 4
    Dernier message: 19/07/2004, 17h07

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