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 :

cellules avec condition relié avec checkbox


Sujet :

Macros et VBA Excel

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Février 2008
    Messages
    10
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 10
    Points : 4
    Points
    4
    Par défaut cellules avec condition relié avec checkbox
    Bonjour,
    Je suis débutant en vba. Voici mon problème. Je veux quand je remplir les cellules a1,a2 et a3 et que si les conditions sont vrai que le checkbox3 se coche automatiquement. Si les conditions sont fausses que le checkbox4 se coche. merci

    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
    Sub Macro1()
    '
    ' Macro1 Macro
    cond1 = Range("a1") <= 10
    cond2 = Range("a2") <= 10
    cond3 = Range("a3") >= 10
     
    If cond1 And cond2 And cond3 = True Then
    CheckBox3.Value = True 
    CheckBox4.Value = False
    End If
     
    If cond1 And cond2 = True And cond3 = False Then
    CheckBox3.Value = false
    CheckBox4.Value = True
    End If
     
    If cond1 And cond3 = True And cond2 = False Then
    CheckBox3.Value = false
    CheckBox4.Value = True
    End If
     
    If cond1 = True And cond2 And cond3 = False Then
    CheckBox3.Value = false
    CheckBox4.Value = True
     
    End If
     
    If cond1 And cond2 And cond3 = False Then
    CheckBox3.Value = false
    CheckBox4.Value = True
     
    End If
     
    If cond1 And cond2 = False And cond3 = True Then
    CheckBox3.Value = False
    CheckBox4.Value = True
    End If
     
    If cond1 And cond3 = False And cond2 = True Then
    CheckBox3.Value = False
    CheckBox4.Value = True
    End If
     
    If cond1 = False And cond2 And cond3 = True Then
    CheckBox3.Value = False
    CheckBox4.Value = True
    End If
     
    End Sub
     
    Private Sub Worksheet_change(ByVal taget As Excel.Range)
    If Range("a1") Or Range("a2") Or Range("a3") <> 0 Then
    Run Macro1
    End If
    If Range("a1") Or Range("a2") Or Range("a3") = 0 Then
     
    Run Macro1
    End If
     
    End Sub

  2. #2
    Expert éminent
    Avatar de fring
    Homme Profil pro
    Engineering
    Inscrit en
    Février 2008
    Messages
    3 900
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    Localisation : Belgique

    Informations professionnelles :
    Activité : Engineering

    Informations forums :
    Inscription : Février 2008
    Messages : 3 900
    Points : 7 964
    Points
    7 964
    Par défaut
    B'jour,

    J'ai pas regardé dans les détails ce qui pourrait être simplifié, j'ai juste adapté ton code pour que (en principe ) il fonctionne
    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
    Sub Macro1()
    Dim Cond1 As Boolean, Cond2 As Boolean, Cond3 As Boolean
     
    Cond1 = False
    Cond2 = False
    Cond3 = False
     
    If Range("A1") <= 10 Then Cond1 = True
    If Range("A2") <= 10 Then Cond2 = True
    If Range("A3") >= 10 Then Cond3 = True
     
     
    If Cond1 = True And Cond2 = True And Cond3 = True Then
    ActiveSheet.OLEObjects("CheckBox3").Object.Value = True
    ActiveSheet.OLEObjects("CheckBox4").Object.Value = False
    End If
     
    If Cond1 = True And Cond2 = True And Cond3 = False Then
    ActiveSheet.OLEObjects("CheckBox3").Object.Value = False
    ActiveSheet.OLEObjects("CheckBox4").Object.Value = True
    End If
     
    If Cond1 = True And Cond3 = True And Cond2 = False Then
    ActiveSheet.OLEObjects("CheckBox3").Object.Value = False
    ActiveSheet.OLEObjects("CheckBox4").Object.Value = True
    End If
     
    If Cond1 = True And Cond2 = False And Cond3 = False Then
    ActiveSheet.OLEObjects("CheckBox3").Object.Value = False
    ActiveSheet.OLEObjects("CheckBox4").Object.Value = True
    End If
     
    If Cond1 = False And Cond2 = False And Cond3 = False Then
    ActiveSheet.OLEObjects("CheckBox3").Object.Value = False
    ActiveSheet.OLEObjects("CheckBox4").Object.Value = True
    End If
     
    If Cond1 = False And Cond2 = False And Cond3 = True Then
    ActiveSheet.OLEObjects("CheckBox3").Object.Value = False
    ActiveSheet.OLEObjects("CheckBox4").Object.Value = True
    End If
     
    If Cond1 = False And Cond3 = False And Cond2 = True Then
    ActiveSheet.OLEObjects("CheckBox3").Object.Value = False
    ActiveSheet.OLEObjects("CheckBox4").Object.Value = True
    End If
     
    If Cond1 = False And Cond2 = True And Cond3 = True Then
    ActiveSheet.OLEObjects("CheckBox3").Object.Value = False
    ActiveSheet.OLEObjects("CheckBox4").Object.Value = True
    End If
     
    End Sub

  3. #3
    Membre averti Avatar de tomy7
    Profil pro
    Étudiant
    Inscrit en
    Janvier 2008
    Messages
    540
    Détails du profil
    Informations personnelles :
    Âge : 37
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2008
    Messages : 540
    Points : 391
    Points
    391
    Par défaut
    peut etre mieu de faire ca comme ceci je ne sais pas ce qu en pense fring...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    If Range("A1") <= 10 Then Cond1 = True
    ElseIf Range("A2") <= 10 Then Cond2 = True
    ElseIf Range("A3") >= 10 Then Cond3 = True
    End If

  4. #4
    Candidat au Club
    Profil pro
    Inscrit en
    Février 2008
    Messages
    10
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 10
    Points : 4
    Points
    4
    Par défaut
    Merci beaucoup ! La Macro1 fonctionne très bien.

    Est-ce possible que la macro1 fonctionne automatiquement en remplissant les cellule a1 ,a2 et a3., avec ce code.:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    Private Sub Worksheet_change(ByVal taget As Excel.Range)
    If Range("a1") Or Range("a2") Or Range("a3") <> 0 Then
    Run Macro1
    End If
    If Range("a1") Or Range("a2") Or Range("a3") = 0 Then
     
    Run Macro1
    End If

  5. #5
    Expert éminent
    Avatar de fring
    Homme Profil pro
    Engineering
    Inscrit en
    Février 2008
    Messages
    3 900
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    Localisation : Belgique

    Informations professionnelles :
    Activité : Engineering

    Informations forums :
    Inscription : Février 2008
    Messages : 3 900
    Points : 7 964
    Points
    7 964
    Par défaut
    Citation Envoyé par tomy7 Voir le message
    peut etre mieu de faire ca comme ceci je ne sais pas ce qu en pense fring...
    Non pas correct, déjà écrit comme ça tu vas avoir un joli bug
    Donc si je reprend ton idée
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    If Range("a1") <= 10 Then
    Range("b1") = "ok"
    ElseIf Range("a2") <= 10 Then
    Range("b2") = "ok"
    ElseIf Range("a3") <= 10 Then
    Range("b3") = "ok"
    End If
    Le résultat sera que si la première condition est Vrai, il ne vérifiera pas les conditions 2 et 3.

  6. #6
    Membre averti Avatar de tomy7
    Profil pro
    Étudiant
    Inscrit en
    Janvier 2008
    Messages
    540
    Détails du profil
    Informations personnelles :
    Âge : 37
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2008
    Messages : 540
    Points : 391
    Points
    391
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    Private Sub Worksheet_change(ByVal taget As Excel.Range)
    If Range("a1").value = 0 Or Range("a2").value = 0 Or Range("a3").value = 0Then
    application.Run "book1!Macro1" ' par exemple
    End If
    end sub
    tu vois que sa marche fring, non je deconne c est plus correcte comme tu l as ecris!

  7. #7
    Expert éminent
    Avatar de fring
    Homme Profil pro
    Engineering
    Inscrit en
    Février 2008
    Messages
    3 900
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    Localisation : Belgique

    Informations professionnelles :
    Activité : Engineering

    Informations forums :
    Inscription : Février 2008
    Messages : 3 900
    Points : 7 964
    Points
    7 964
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$A$1" Or Target.Address = "$A$2" Or Target.Address = "$A$3" Then Macro1
    End Sub

  8. #8
    Membre émérite
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    2 130
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 2 130
    Points : 2 443
    Points
    2 443
    Par défaut
    Salutlavalois et le forum
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    Private Sub Worksheet_Change(ByVal Target As Range)
    If [A1] <= 10 And [A2] <= 10 And [A3] >= 10 Then
        Me.CheckBox3.Value = True
        Me.CheckBox4.Value = False
    Else
        Me.CheckBox4.Value = True
        Me.CheckBox3.Value = false
    End If
    End Sub
    A+

  9. #9
    Candidat au Club
    Profil pro
    Inscrit en
    Février 2008
    Messages
    10
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 10
    Points : 4
    Points
    4
    Par défaut
    Merci Fring pour ton aide,

    Pour faire fonctionner automatiquement la macro1 quand les 3 cellules ont une valeur le code suivant n'a pas fonctionné.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     Private Sub Worksheet_change(ByVal taget As Excel.Range)
    If Range("a1") Or Range("a2") Or Range("a3") <> 0 Then
    call Macro1
    End If
    If Range("a1") Or Range("a2") Or Range("a3") = 0 Then
     
    call Macro1
    End If
    J'ai testé celui que tu m'as envoyé
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$A$1" Or Target.Address = "$A$2" Or Target.Address = "$A$3" Then Macro1
    End Sub
    et il ne fonctionne pas plus lui non plus.

    J' aimerai quand les 3 cellules sont remplies que la macro fonctionne automatiquement.

    merci

  10. #10
    Expert éminent
    Avatar de fring
    Homme Profil pro
    Engineering
    Inscrit en
    Février 2008
    Messages
    3 900
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    Localisation : Belgique

    Informations professionnelles :
    Activité : Engineering

    Informations forums :
    Inscription : Février 2008
    Messages : 3 900
    Points : 7 964
    Points
    7 964
    Par défaut
    Essaye le code de Gorfael "tout en un"

    Si il te convient, tu peux virer la macro1

  11. #11
    Candidat au Club
    Profil pro
    Inscrit en
    Février 2008
    Messages
    10
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 10
    Points : 4
    Points
    4
    Par défaut
    salut Fring

    Je l'ai essayé mais il ne fonctionne pas non plus. Comme je commence en vba je vais faire des recherche pour trouver la solution.

    merci beaucoup pour ton aide.

  12. #12
    Membre averti Avatar de tomy7
    Profil pro
    Étudiant
    Inscrit en
    Janvier 2008
    Messages
    540
    Détails du profil
    Informations personnelles :
    Âge : 37
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2008
    Messages : 540
    Points : 391
    Points
    391
    Par défaut
    essaye :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    application.run "Macro1"
    c est ce que m a donne l enregistreur!

  13. #13
    Expert éminent
    Avatar de fring
    Homme Profil pro
    Engineering
    Inscrit en
    Février 2008
    Messages
    3 900
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    Localisation : Belgique

    Informations professionnelles :
    Activité : Engineering

    Informations forums :
    Inscription : Février 2008
    Messages : 3 900
    Points : 7 964
    Points
    7 964
    Par défaut
    ça devrait pourtant fonctionner

    Bon...en gardant ta Macro1, essaye ceci alors
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    Private Sub Worksheet_change(ByVal taget As Excel.Range)
    If Range("A1") <> "" And Range("A2") <> "" And Range("A3") <> "" Then
    Macro1
    End If
    End Sub
    Pour info ce code ci ou le code de Gorfael ne doivent pas se placer dans un Module mais directement dans l'objet Feuil concernée

  14. #14
    Expert éminent
    Avatar de cafeine
    Inscrit en
    Juin 2002
    Messages
    3 904
    Détails du profil
    Informations forums :
    Inscription : Juin 2002
    Messages : 3 904
    Points : 6 781
    Points
    6 781
    Par défaut
    Hello,

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    Private Sub Worksheet_Change(ByVal Target As Range)
     
        Me.CheckBoxes("Check Box 3").Value = _
                (Range("A1") < 10 And (Range("A2") < 10)) _
                        And (Range("A3") > 10)
     
     
    End Sub

  15. #15
    Candidat au Club
    Profil pro
    Inscrit en
    Février 2008
    Messages
    10
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 10
    Points : 4
    Points
    4
    Par défaut
    Non Fring, ça fonctionne pas plus, comme je suis débutant je ne comprend pas tout. Je dois faire des recherche sur internet pour m'aider à trouver et à comprendre le problème.

    merci

  16. #16
    Expert éminent
    Avatar de fring
    Homme Profil pro
    Engineering
    Inscrit en
    Février 2008
    Messages
    3 900
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    Localisation : Belgique

    Informations professionnelles :
    Activité : Engineering

    Informations forums :
    Inscription : Février 2008
    Messages : 3 900
    Points : 7 964
    Points
    7 964
    Par défaut
    Pourtant toutes les solutions proposées fonctionnent...

    Ci-joint un exemple avec le code de Gorfael

  17. #17
    Membre émérite
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    2 130
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 2 130
    Points : 2 443
    Points
    2 443
    Par défaut
    Salut lavalois
    comme je suis débutant je ne comprend pas tout
    Les macros données ne fonctionnent pas sur un module général : il faut que se soit le module lié à la feuille.
    Clic-droit sur le nom de l'onglet>>Menu contextuel>>Visualiser le code

    Si ce n'est pas ça, désolé
    A+

  18. #18
    Inactif  
    Avatar de ouskel'n'or
    Profil pro
    Inscrit en
    Février 2005
    Messages
    12 464
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 12 464
    Points : 15 546
    Points
    15 546
    Par défaut
    Si tes checkbox sont dans la feuille de calculs
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    Private Sub Worksheet_change(ByVal target As Excel.Range)
    Dim Cond1 As Boolean, cond2 As Boolean, cond3 As Boolean
        Cond1 = Range("a1") <= 10
        cond2 = Cond1 And Range("a2") <= 10
        cond3 = cond2 And Range("a3") >= 10
        ActiveSheet.CheckBox3 = cond3
        ActiveSheet.CheckBox4 = Not cond3
    End Sub
    Si tes checkbox sont dans un userform
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    Sub CommandButton1_Click() 'A adapter selon la façon d'actualiser tes données
    Dim Cond1 as boolean, cond2 as boolean, cond3 as boolean
        cond1 = Range("a1") <= 10
        cond2 = Cond1 and Range("a2") <= 10
        cond3 = Cond2 and Range("a3") >= 10 
        Me.CheckBox3 = Cond3
        Me.CheckBox4 = not Cond3
    End Sub
    A aucun moment tu n'indiques comment tu saisis tes données
    Ton premier code laisse penser que tes checkbox sont dans un userform.
    Ce que tu dis par la suite incite à penser qu'ils sont dans une feuille de calculs.
    Une précision sur ces trois points pourrait aider à comprendre de que tu tentes d'obtenir.
    J'ai testé le code que je te propose

    En plus court, sans variable
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    Private Sub Worksheet_change(ByVal target As Excel.Range)
          ActiveSheet.CheckBox3 = IIf(Range("a1") <= 10 And Range("a2") <= 10 And Range("a3") >= 10, True, False)
          ActiveSheet.CheckBox4 = Not ActiveSheet.CheckBox3
    End Sub

Discussions similaires

  1. Réponses: 25
    Dernier message: 22/09/2014, 12h37
  2. Aide avec fonction SUM avec condition
    Par Beaudelicius dans le forum Langage SQL
    Réponses: 2
    Dernier message: 11/01/2012, 22h30
  3. [XL-2003] VBa selection cellules avec condition
    Par gwencab dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 06/01/2011, 16h49
  4. repositionnement d'une cellule avec condition
    Par commetuveux dans le forum Conception
    Réponses: 9
    Dernier message: 20/10/2009, 11h57
  5. Moyenne avec condition sur plage de cellule
    Par emilie31 dans le forum Excel
    Réponses: 2
    Dernier message: 18/11/2008, 14h08

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