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 :

Dans une fonction, comment tester plusieurs valeurs d'une seule cellule


Sujet :

Macros et VBA Excel

  1. #1
    Membre du Club
    Inscrit en
    Janvier 2010
    Messages
    84
    Détails du profil
    Informations forums :
    Inscription : Janvier 2010
    Messages : 84
    Points : 47
    Points
    47
    Par défaut Dans une fonction, comment tester plusieurs valeurs d'une seule cellule
    Bonjour,

    J'ai une fonction qui calcule le status d'une entrée en fonction de plusieurs de ces valeurs, en gros elle se présente comme ceci:
    =CalculateStatus(A2;C2;K2;L2;AH2)

    Dans cette fonction, en fonction des différentes valeurs, j'assigne un status, jusque là ça va.
    Il y a notamment un test:
    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
     
    Function CalculateStatus(ASRType As String, ASRState As String, ASRCN As String, ASRPbLinked As String, CN_state As String)
     
    Dim strResult As String
     
    ..........
            If ASRState = "Waiting" Then
                If ASRCN = "" Then
                    strResult = "ERROR1"
                    ' ERROR1 means that: an ASR-Problem could not be in Waiting state without having a CN attached.
                Else
                    If Range("ASR_CN!B:B").Find(ASRCN) Is Nothing Then
                        strResult = "ERROR2"
                        ' ERROR2 means that: ASR-CN number is not found in the ASR_CN sheet.
                    Else
                        strResult = "Waiting for resolution"
                    End If
          ...........
    Jusque là tout va bien quand mon entrée ASRCN, donc K2, contient unes seule valeur.
    Elle se présente sous la forme ASR-66 par exemple, là je n'ai pas de soucis pour vérifier si elle existe ou pas dans mon autre feuille.

    Par contre mon problème aujourd'hui est que je peux avoir désormais plusieurs "ASRCN" dans K2.
    Du type: "ASR-66,ASR-67".
    Il peut y en avoir plus de deux.

    Ma question est donc, comment faire ce même test sur les différentes valeurs contenu dans K2, les valeurs étant séparé par des virgules.
    Dés que l'une d'entre est trouvé dans mon autre feuille, c bon, pas besoin de vérifier les autres.

    Je sais pas si c'est très clair

    Par avance merci

  2. #2
    Membre du Club
    Profil pro
    Étudiant
    Inscrit en
    Juillet 2011
    Messages
    30
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2011
    Messages : 30
    Points : 42
    Points
    42
    Par défaut
    Bonjour, à essayer en utilisant les fonctions Right, Mid et Left :

    Left(ASRCN, 6) donne les 6 premiers caractères. Right(ASRCN, 6) donne les 6 derniers. Mid(ASRCN, 7, 6) donne les 6 caractères en partant du 7ème caractère inclus. (je sais pas si c'est clair)

    A voir si tu peux adapter.

    Cordialement,
    Jordan

  3. #3
    Membre du Club
    Inscrit en
    Janvier 2010
    Messages
    84
    Détails du profil
    Informations forums :
    Inscription : Janvier 2010
    Messages : 84
    Points : 47
    Points
    47
    Par défaut
    Merci pour ta réponse Jordan.
    J'y avais penser, mais à terme, je vais avoir des ASR-565, des ASR-7678.... et là ca marche plus.

    N'y a-t-il pas un moyen de dire, je regarde chaque valeur séparé par la virgule?

  4. #4
    Membre du Club
    Profil pro
    Étudiant
    Inscrit en
    Juillet 2011
    Messages
    30
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2011
    Messages : 30
    Points : 42
    Points
    42
    Par défaut
    Rebonjour,

    Effetivement ça risque de poser souci. Je ne sais pas vraiment si c'est possible de faire cela, désolé. A voir avec quelqu'un de plus expérimenté que moi. ^^

    Cordialement,
    Jordan

  5. #5
    Modérateur
    Avatar de AlainTech
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Mai 2005
    Messages
    4 235
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 70
    Localisation : Belgique

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : Finance

    Informations forums :
    Inscription : Mai 2005
    Messages : 4 235
    Points : 24 327
    Points
    24 327
    Par défaut
    Bonjour à tous,

    Un petit coup de F1 sur la fonction Split...
    N'oubliez pas de cliquer sur quand vous avez obtenu ou trouvé vous-même la réponse à votre question.
    Si vous trouvez seul, pensez à poster votre solution. Elle peut servir à d'autres!
    Pensez aussi à voter pour les réponses qui vous ont aidés.
    ------------
    Je dois beaucoup de mes connaissances à mes erreurs!

  6. #6
    Membre du Club
    Inscrit en
    Janvier 2010
    Messages
    84
    Détails du profil
    Informations forums :
    Inscription : Janvier 2010
    Messages : 84
    Points : 47
    Points
    47
    Par défaut
    Merci, c'est exactement ce que je cherchais.
    Par contre, sans avoir changer le test que je faisais, le FIND, ca plante, c'est étrange.

    Voilà à quoi ressmeble mon nouveau 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
     
    Function CalculateStatus(ASRType As String, ASRState As String, ASRCN As String, ASRPbLinked As String, CN_state As String)
     
    Dim strResult As String
    Dim ASRCN_values() As Variant
    Dim I As Integer
     
    strResult = "not calculated"
     
       If ASRType = "ASR Problem" Then
            If ASRState = "Open" Or ASRState = "Assigned" Or ASRState = "In Progress" Or ASRState = "Pending Customer" Or ASRState = "Waiting for Approval" Then
                strResult = "In progress"
            Else
            If ASRState = "Waiting" Then
                If ASRCN = "" Then
                    strResult = "ERROR1"
                Else
                    ASRCN_values() = Split(ASRCN, ", ")
                    If UBound(ASRCN_values) > 0 Then
                        For I = 0 To UBound(ASRCN_values)
                            If strResult = "Waiting for CN resolution" Then Exit For
                                If Range("ASR_CN!B:B").Find(ASRCN_values(I)) Is Nothing Then
                                    strResult = "ERROR2"
                                Else
                                    strResult = "Waiting for CN resolution"
                                End If
                        Next
                    Else
                        If Range("ASR_CN!B:B").Find(ASRCN_values(0)) Is Nothing Then
                            strResult = "ERROR2"
                        Else
                            strResult = "Waiting for CN resolution"
                        End If
                    End If
                End If
            Else
    ..................
    Il semble que Range("ASR_CN!B:B").Find(ASRCN_values(I)) ne fonctionne pas, comme Range("ASR_CN!B:B").Find(ASRCN_values(0)) d'ailleurs.
    alors que si je reviens à mon ancien code Range("ASR_CN!B:B").Find(ASRCN), ça fonctionne.

    Alors que dans certains cas Range("ASR_CN!B:B").Find(ASRCN)=Range("ASR_CN!B:B").Find(ASRCN_values(0))

    Une idée du pourquoi du comment?

    Est-ce que cela pourrait venir du fait que j'utilise ASRCN_values() As Variant!

  7. #7
    Membre du Club
    Inscrit en
    Janvier 2010
    Messages
    84
    Détails du profil
    Informations forums :
    Inscription : Janvier 2010
    Messages : 84
    Points : 47
    Points
    47
    Par défaut
    Bon, j'ai légèrement modifié ma façon de faire le test, mais ça plante toujours qd je passe par ma boucle FOR à priori

    ma fonction en entier est:
    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
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
     
     
    Function Look(strWhat As String, strSheet As String, strRange As String)
      Dim celF As Range
      With Worksheets(strSheet).Range(strRange)
       Set celF = .Find(strWhat, Lookat:=xlWhole)
       If Not celF Is Nothing Then
         Look = "LookOK"
        Else
         Look = "LookKO"
       End If
      End With
    End Function
     
    Function CalculateStatus(ASRType As String, ASRState As String, ASRCN As String, ASRPbLinked As String, CN_state As String)
     
    Dim strResult As String
    Dim ASRCN_values As Variant
    Dim I As Integer
    Dim test As String
     
    strResult = "not calculated"
     
       If ASRType = "ASR Problem" Then
            If ASRState = "Open" Or ASRState = "Assigned" Or ASRState = "In Progress" Or ASRState = "Pending Customer" Or ASRState = "Waiting for Approval" Then
                strResult = "In progress"
            Else
            If ASRState = "Waiting" Then
                If ASRCN = "" Then
                    strResult = "ERROR1"
                Else
                ASRCN_values = Split(ASRCN, ", ")
                If UBound(ASRCN_values) > 0 Then
                    For I = 0 To UBound(ASRCN_values)
                    If strResult = "Waiting for CN resolution" Then Exit For
                    test = ASRCN_values(I)
                    If Look(test, "ASR_CN", "B:B") = "LookKO" Then
                        strResult = "ERROR2"
                        ' ERROR2 means that: 
                    Else
                        strResult = "Waiting for CN resolution"
                    End If
                    Next
                Else
                test = ASRCN_values(0)
                If Look(test, "ASR_CN", "B:B") = "LookKO" Then
                    strResult = "ERROR2"
                    ' ERROR2 means that: 
                Else
                    strResult = "Waiting for CN resolution"
                End If
                End If
                End If
            Else
            If ASRState = "Resolved" Or ASRState = "Closed" Then
                If ASRCN = "" Then
                    strResult = "Closed without CN"
                Else
                    If Range("ASR_CN!B:B").Find(ASRCN) Is Nothing Then
                        strResult = "ERROR3"
                        ' ERROR3 means that: 
                    Else
                        If CN_state = "Rejected" Then
                            strResult = "Closed without CN"
                        Else
                        strResult = "Closed with CN resolution"
                        End If
                    End If
                End If
            End If
            End If
            End If
        Else
        If ASRType = "ASR Incident" Then
            If ASRState = "Open" Or ASRState = "Assigned" Or ASRState = "In Progress" Or ASRState = "Pending Customer" Then
                strResult = "In progress"
            Else
            If ASRState = "Waiting" Then
                If ASRPbLinked = "" Then
                    strResult = "ERROR5"
                    'ERROR5 means that: 
                Else
                Dim ASRPbLinked_state As Variant
                ASRPbLinked_state = Application.VLookup(ASRPbLinked, Range("B:W"), 22, False)
     
                If ASRPbLinked_state = "Waiting for CN resolution" Then
                    strResult = "Waiting for CN resolution"
                Else
                If ASRPbLinked_state = "In progress" Then
                    strResult = "Linked to an opened Problem"
                Else
                If ASRPbLinked_state = "Closed without CN" Or ASRPbLinked_state = "Closed with CN resolution" Then
                    strResult = "ERROR6"
                    'ERROR6 means that: 
                Else
                If ASRPbLinked_state = "ERROR1" Or ASRPbLinked_state = "ERROR2" Or ASRPbLinked_state = "ERROR3" Then
                    strResult = "ERROR7"
                    'ERROR7 means that: 
                End If
                End If
                End If
                End If
                End If
            Else
            If ASRState = "Resolved" Or ASRState = "Closed" Then
                If ASRPbLinked = "" Then
                    strResult = "Closed without Problem"
                Else
                    Dim ASRPbLinked_status As Variant
                    Dim ASRPbLinked_CN As Variant
     
                    ASRPbLinked_status = Application.VLookup(ASRPbLinked, Range("B:C"), 2, False)
                    ASRPbLinked_CN = Application.VLookup(ASRPbLinked, Range("B:K"), 10, False)
     
                If ASRPbLinked_status = "Resolved" Or ASRPbLinked_status = "Closed" Then
                    If ASRPbLinked_CN = "" Then
                        strResult = "Closed without CN"
                    Else
                    If Range("ASR_CN!B:B").Find(ASRPbLinked_CN) Is Nothing Then
                        strResult = "ERROR3"
                        ' ERROR3 means that: 
                    Else
                        If CN_state = "Rejected" Then
                            strResult = "Closed without CN"
                        Else
                        strResult = "Closed with CN resolution"
                        End If
                    End If
                    End If
                Else
                    strResult = "ERROR4"
                    ' ERROR4 means that: 
                End If
                End If
            End If
            End If
            End If
        End If
        End If
     
        CalculateStatus = strResult
     
    End Function
    Quand je la lance =CalculateStatus(A33;C33;K33;L33;AH33), j'obtiens l'erreur #VALUE!

    Alors que si je crée une sub de test comme 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
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    Sub test()
     
    Dim ASRType As String
    ASRType = "ASR Problem"
    Dim ASRState As String
    ASRState = "Waiting"
    Dim ASRCN As String
    ASRCN = "ASR-374, ASR-419"
     
     
    Dim strResult As String
    Dim ASRCN_values As Variant
    Dim I As Integer
    Dim test As String
     
    strResult = "not calculated"
     
       If ASRType = "ASR Problem" Then
            If ASRState = "Open" Or ASRState = "Assigned" Or ASRState = "In Progress" Or ASRState = "Pending Customer" Or ASRState = "Waiting for Approval" Then
                strResult = "In progress"
            Else
            If ASRState = "Waiting" Then
                If ASRCN = "" Then
                    strResult = "ERROR1"
                    ' ERROR1 means that: 
                Else
                ASRCN_values = Split(ASRCN, ", ")
                If UBound(ASRCN_values) > 0 Then
                    For I = 0 To UBound(ASRCN_values)
                    If strResult = "Waiting for CN resolution" Then Exit For
                    test = ASRCN_values(I)
                    If Look(test, "ASR_CN", "B:B") = "LookKO" Then
                        strResult = "ERROR2"
                        ' ERROR2 means that: 
                    Else
                        strResult = "Waiting for CN resolution"
                    End If
                    Next
                Else
                test = ASRCN_values(0)
                If Look(test, "ASR_CN", "B:B") = "LookKO" Then
                    strResult = "ERROR2"
                    ' ERROR2 means that: 
                Else
                    strResult = "Waiting for CN resolution"
                End If
                End If
                End If
            Else
            If ASRState = "Resolved" Or ASRState = "Closed" Then
                If ASRCN = "" Then
                    strResult = "Closed without CN"
                Else
                    If Range("ASR_CN!B:B").Find(ASRCN) Is Nothing Then
                        strResult = "ERROR3"
                        ' ERROR3 means that:
                    Else
                        If CN_state = "Rejected" Then
                            strResult = "Closed without CN"
                        Else
                        strResult = "Closed with CN resolution"
                        End If
                    End If
                End If
            End If
            End If
            End If
        Else
        If ASRType = "ASR Incident" Then
            If ASRState = "Open" Or ASRState = "Assigned" Or ASRState = "In Progress" Or ASRState = "Pending Customer" Then
                strResult = "In progress"
            Else
            If ASRState = "Waiting" Then
                If ASRPbLinked = "" Then
                    strResult = "ERROR5"
                    'ERROR5 means that: 
                Else
                Dim ASRPbLinked_state As Variant
                ASRPbLinked_state = Application.VLookup(ASRPbLinked, Range("B:W"), 22, False)
     
                If ASRPbLinked_state = "Waiting for CN resolution" Then
                    strResult = "Waiting for CN resolution"
                Else
                If ASRPbLinked_state = "In progress" Then
                    strResult = "Linked to an opened Problem"
                Else
                If ASRPbLinked_state = "Closed without CN" Or ASRPbLinked_state = "Closed with CN resolution" Then
                    strResult = "ERROR6"
                    'ERROR6 means that:
                Else
                If ASRPbLinked_state = "ERROR1" Or ASRPbLinked_state = "ERROR2" Or ASRPbLinked_state = "ERROR3" Then
                    strResult = "ERROR7"
                    'ERROR7 means that:
                End If
                End If
                End If
                End If
                End If
            Else
            If ASRState = "Resolved" Or ASRState = "Closed" Then
                If ASRPbLinked = "" Then
                    strResult = "Closed without Problem"
                Else
                    Dim ASRPbLinked_status As Variant
                    Dim ASRPbLinked_CN As Variant
     
                    ASRPbLinked_status = Application.VLookup(ASRPbLinked, Range("B:C"), 2, False)
                    ASRPbLinked_CN = Application.VLookup(ASRPbLinked, Range("B:K"), 10, False)
     
                If ASRPbLinked_status = "Resolved" Or ASRPbLinked_status = "Closed" Then
                    If ASRPbLinked_CN = "" Then
                        strResult = "Closed without CN"
                    Else
                    If Range("ASR_CN!B:B").Find(ASRPbLinked_CN) Is Nothing Then
                        strResult = "ERROR3"
                        ' ERROR3 means that: 
                    Else
                        If CN_state = "Rejected" Then
                            strResult = "Closed without CN"
                        Else
                        strResult = "Closed with CN resolution"
                        End If
                    End If
                    End If
                Else
                    strResult = "ERROR4"
                    ' ERROR4 means that:
                End If
                End If
            End If
            End If
            End If
        End If
        End If
     
       Sheets("ASR_IP_Data").Select
        Range("L340").Select
       ActiveCell.FormulaR1C1 = strResult
     
    End Sub
    Dans ce cas là, je rentre directement les valeurs de mes cellules en questions:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    Dim ASRType As String
    ASRType = "ASR Problem"
    Dim ASRState As String
    ASRState = "Waiting"
    Dim ASRCN As String
    ASRCN = "ASR-374, ASR-419"
    et là, j'obtiens le bon résultat en L340 en l'occurence, qui est "Waiting for CN resolution".

    je ne vois pas pourquoi dans le cas de la fonction, ça ne marche pas.
    Si mon ASRCN a une seule "valeur" comme ASR-419, pas de soucis, le fonction marche bien, elle passe par le test
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    If UBound(ASRCN_values) > 0
    puis
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Else
                test = ASRCN_values(0)
                If Look(test, "ASR_CN", "B:B") = "LookKO" Then
    et le résultat final est cohérent.

    Par contre, dés le le nombre de valeur est supérieur à 1 et qu'il passe donc par la boucle FOR, ça plante!!!

    une idée

  8. #8
    Membre du Club
    Inscrit en
    Janvier 2010
    Messages
    84
    Détails du profil
    Informations forums :
    Inscription : Janvier 2010
    Messages : 84
    Points : 47
    Points
    47
    Par défaut
    pas de nouvelles idées sur mon cas

    par avance merci

Discussions similaires

  1. Réponses: 3
    Dernier message: 13/11/2008, 09h21
  2. Réponses: 1
    Dernier message: 16/03/2007, 17h30
  3. Réponses: 14
    Dernier message: 09/03/2007, 12h21
  4. Réponses: 3
    Dernier message: 23/01/2007, 09h20
  5. [TP 7] Comment mettre plusieurs valeurs en une ?
    Par Qawox dans le forum Turbo Pascal
    Réponses: 7
    Dernier message: 15/07/2006, 02h38

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