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 :

Combinaison de cases à cocher pour afficher un résultat


Sujet :

Macros et VBA Excel

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Mai 2012
    Messages
    121
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Mai 2012
    Messages : 121
    Points : 78
    Points
    78
    Par défaut Combinaison de cases à cocher pour afficher un résultat
    Bonjour amis du forum

    J'ai un problème étrange avec le code que j'ai créé.
    Mon projet propose à une personne de cocher des cases parmi un choix de neuf, et selon la combinaison obtenue, un score s'affiche.
    Mon problème est celui-ci: lorsque je coche la case 6 et ensuite la case 1, le score 9 s'affiche (ce qui est correcte), mais lorsque je fais le contraire, (cocher la case 1 en premier lieu et ensuite la case 6), rien ne se passe!!!

    Voici le 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
    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
    Private Sub CheckBox1_Click()
     
     
            'Case #1
             If (CheckBox1.Value = True) And _
                (CheckBox2.Value = False) And _
                (CheckBox3.Value = False) And _
                (CheckBox4.Value = False) And _
                (CheckBox5.Value = False) And _
                (CheckBox6.Value = True) And _
                (CheckBox7.Value = False) And _
                (CheckBox8.Value = False) And _
                (CheckBox9.Value = False) Then
                Range("M9").Value = 9
     
            'Case #2
            Else
            If (CheckBox1.Value = True) And _
                (CheckBox2.Value = False) And _
                (CheckBox3.Value = False) And _
                (CheckBox4.Value = False) And _
                (CheckBox5.Value = False) And _
                (CheckBox6.Value = False) And _
                (CheckBox7.Value = True) And _
                (CheckBox8.Value = False) And _
                (CheckBox9.Value = False) Then
                Range("M9").Value = 8
     
            'Case #3
            Else
            If (CheckBox1.Value = False) And _
                (CheckBox2.Value = True) And _
                (CheckBox3.Value = False) And _
                (CheckBox4.Value = False) And _
                (CheckBox5.Value = False) And _
                (CheckBox6.Value = True) And _
                (CheckBox7.Value = False) And _
                (CheckBox8.Value = False) And _
                (CheckBox9.Value = False) Then
                Range("M9").Value = 7
     
            'Case #4
            Else
            If (CheckBox1.Value = False) And _
                (CheckBox2.Value = True) And _
                (CheckBox3.Value = False) And _
                (CheckBox4.Value = False) And _
                (CheckBox5.Value = False) And _
                (CheckBox6.Value = False) And _
                (CheckBox7.Value = True) And _
                (CheckBox8.Value = False) And _
                (CheckBox9.Value = False) Then
                Range("M9").Value = 6
     
            'Case #5
            Else
            If (CheckBox1.Value = True) And _
                (CheckBox2.Value = False) And _
                (CheckBox3.Value = False) And _
                (CheckBox4.Value = False) And _
                (CheckBox5.Value = False) And _
                (CheckBox6.Value = False) And _
                (CheckBox7.Value = False) And _
                (CheckBox8.Value = False) And _
                (CheckBox9.Value = True) Then
                Range("M9").Value = 5
     
            'Case #6
            Else
            If (CheckBox1.Value = True) And _
                (CheckBox2.Value = False) And _
                (CheckBox3.Value = False) And _
                (CheckBox4.Value = False) And _
                (CheckBox5.Value = False) And _
                (CheckBox6.Value = False) And _
                (CheckBox7.Value = True) And _
                (CheckBox8.Value = False) And _
                (CheckBox9.Value = True) Then
                Range("M9").Value = 4
     
            'Case #7
            Else
            If (CheckBox1.Value = False) And _
                (CheckBox2.Value = True) And _
                (CheckBox3.Value = False) And _
                (CheckBox4.Value = False) And _
                (CheckBox5.Value = False) And _
                (CheckBox6.Value = False) And _
                (CheckBox7.Value = False) And _
                (CheckBox8.Value = False) And _
                (CheckBox9.Value = True) Then
                Range("M9").Value = 3
     
            'Case #8
            Else
            If (CheckBox1.Value = False) And _
                (CheckBox2.Value = True) And _
                (CheckBox3.Value = False) And _
                (CheckBox4.Value = False) And _
                (CheckBox5.Value = False) And _
                (CheckBox6.Value = False) And _
                (CheckBox7.Value = True) And _
                (CheckBox8.Value = False) And _
                (CheckBox9.Value = True) Then
                Range("M9").Value = 2
     
            End If
            End If
            End If
            End If
            End If
            End If
            End If
            End If
     
    End Sub
    Merci beaucoup!!!

  2. #2
    Expert éminent
    Homme Profil pro
    Inscrit en
    Août 2010
    Messages
    3 453
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Août 2010
    Messages : 3 453
    Points : 6 871
    Points
    6 871
    Par défaut
    Bonsoir,

    Ta procédure n'est exécutée que quand tu clique sur la case à cocher "CheckBox1" (si tout ton code est là bien sûr -> CheckBox1_Click). Il te faut en faire une procédure indépendante que tu appelle sur l'évènement Click de chaque case à cocher et il n'est pas nécessaire d'utiliser des Else :
    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
     
    Private Sub CheckBox1_Click()
     
        CaseACocher
     
    End Sub
     
    Private Sub CheckBox2_Click()
     
        CaseACocher
     
    End Sub
     
    Private Sub CheckBox3_Click()
     
        CaseACocher
     
    End Sub
     
    'Private Sub CheckBox1_Click()...etc
     
     
    Sub CaseACocher()
     
            'Case #1
             If (CheckBox1.Value = True) And _
                (CheckBox2.Value = False) And _
                (CheckBox3.Value = False) And _
                (CheckBox4.Value = False) And _
                (CheckBox5.Value = False) And _
                (CheckBox6.Value = True) And _
                (CheckBox7.Value = False) And _
                (CheckBox8.Value = False) And _
                (CheckBox9.Value = False) Then
                Range("M9").Value = 9
            End If
     
            'Case #2
            If (CheckBox1.Value = True) And _
                (CheckBox2.Value = False) And _
                (CheckBox3.Value = False) And _
                (CheckBox4.Value = False) And _
                (CheckBox5.Value = False) And _
                (CheckBox6.Value = False) And _
                (CheckBox7.Value = True) And _
                (CheckBox8.Value = False) And _
                (CheckBox9.Value = False) Then
                Range("M9").Value = 8
            End If
     
            'Case #3
            If (CheckBox1.Value = False) And _
                (CheckBox2.Value = True) And _
                (CheckBox3.Value = False) And _
                (CheckBox4.Value = False) And _
                (CheckBox5.Value = False) And _
                (CheckBox6.Value = True) And _
                (CheckBox7.Value = False) And _
                (CheckBox8.Value = False) And _
                (CheckBox9.Value = False) Then
                Range("M9").Value = 7
            End If
     
            'Case #4
            If (CheckBox1.Value = False) And _
                (CheckBox2.Value = True) And _
                (CheckBox3.Value = False) And _
                (CheckBox4.Value = False) And _
                (CheckBox5.Value = False) And _
                (CheckBox6.Value = False) And _
                (CheckBox7.Value = True) And _
                (CheckBox8.Value = False) And _
                (CheckBox9.Value = False) Then
                Range("M9").Value = 6
            End If
     
            'Case #5
            If (CheckBox1.Value = True) And _
                (CheckBox2.Value = False) And _
                (CheckBox3.Value = False) And _
                (CheckBox4.Value = False) And _
                (CheckBox5.Value = False) And _
                (CheckBox6.Value = False) And _
                (CheckBox7.Value = False) And _
                (CheckBox8.Value = False) And _
                (CheckBox9.Value = True) Then
                Range("M9").Value = 5
            End If
     
            'Case #6
            If (CheckBox1.Value = True) And _
                (CheckBox2.Value = False) And _
                (CheckBox3.Value = False) And _
                (CheckBox4.Value = False) And _
                (CheckBox5.Value = False) And _
                (CheckBox6.Value = False) And _
                (CheckBox7.Value = True) And _
                (CheckBox8.Value = False) And _
                (CheckBox9.Value = True) Then
                Range("M9").Value = 4
            End If
     
            'Case #7
            If (CheckBox1.Value = False) And _
                (CheckBox2.Value = True) And _
                (CheckBox3.Value = False) And _
                (CheckBox4.Value = False) And _
                (CheckBox5.Value = False) And _
                (CheckBox6.Value = False) And _
                (CheckBox7.Value = False) And _
                (CheckBox8.Value = False) And _
                (CheckBox9.Value = True) Then
                Range("M9").Value = 3
            End If
     
            'Case #8
            If (CheckBox1.Value = False) And _
                (CheckBox2.Value = True) And _
                (CheckBox3.Value = False) And _
                (CheckBox4.Value = False) And _
                (CheckBox5.Value = False) And _
                (CheckBox6.Value = False) And _
                (CheckBox7.Value = True) And _
                (CheckBox8.Value = False) And _
                (CheckBox9.Value = True) Then
                Range("M9").Value = 2
            End If
     
    End Sub
    Hervé.

  3. #3
    Membre régulier
    Profil pro
    Inscrit en
    Mai 2012
    Messages
    121
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Mai 2012
    Messages : 121
    Points : 78
    Points
    78
    Par défaut
    Bonjour Theze

    Les personnes qui maitrisent ce type de langage m'impressionnent beaucoup!!
    Merci, ton code fonctionne à merveille!!

    Mais comme tu sembles "calé" en la matière, je vais vous poser une seconde question: J'ai besoin de reproduire cette opération une douzaine des fois (9 cases *12). Dois-je reproduire ce long code 12 fois ou y a-t-il une méthode plus rapide??

    Merci

  4. #4
    Expert éminent
    Homme Profil pro
    Inscrit en
    Août 2010
    Messages
    3 453
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Août 2010
    Messages : 3 453
    Points : 6 871
    Points
    6 871
    Par défaut
    Bonjour,

    Il te faut utiliser un module de classe !
    Pour t'aider, il me faudrait en savoir un peu plus. Tes groupes de case à cocher sont dans des cadres (Frames) ou toutes sur la Form ? Il faudrait peut être les nommer de façon pertinente par exemple par groupe avec un préfixe identique.

    En attente, bonne journée

    Hervé.

  5. #5
    Membre régulier
    Profil pro
    Inscrit en
    Mai 2012
    Messages
    121
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Mai 2012
    Messages : 121
    Points : 78
    Points
    78
    Par défaut
    Bonjour Theze
    Je ne suis pas sûr de bien saisir ta question. Mais pour te résumer mon projet, j'ai 153 cases (9 cases de largeurs qui se répètent 17 fois) qui portent les noms suivants: CheckBox1, CheckBox2, CheckBox3, ... , CheckBox153
    Les 17 opérations faites sur les 9 cases sont identiques, alors ton idée de les grouper est intéressante, mais comment...

Discussions similaires

  1. [WD-2010] case a cocher pour afficher ou non un tableau
    Par jules2m dans le forum Word
    Réponses: 1
    Dernier message: 05/07/2012, 16h06
  2. [MySQL] Afficher le détail d'un enregistrement à partir d'une case à cocher pour le modifier
    Par batoule80 dans le forum PHP & Base de données
    Réponses: 1
    Dernier message: 11/10/2008, 11h15
  3. case à cocher pour afficher zone de texte
    Par mioke dans le forum Général JavaScript
    Réponses: 6
    Dernier message: 22/04/2008, 16h05
  4. Réponses: 12
    Dernier message: 03/12/2007, 18h53
  5. case à cocher pour afficher zone de liste
    Par LuLu21 dans le forum IHM
    Réponses: 4
    Dernier message: 25/06/2007, 16h30

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