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 :

Executer un macro selon le résultat de plusieurs céllules


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Homme Profil pro
    Superviseur en transport
    Inscrit en
    Août 2019
    Messages
    104
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Superviseur en transport
    Secteur : Transports

    Informations forums :
    Inscription : Août 2019
    Messages : 104
    Par défaut Executer un macro selon le résultat de plusieurs céllules
    Bonjour,
    J'aimerais créer un macro VBA qui vas être lancer a l'aide d'un bouton, qui vas lancer une autre macro selon le résultat précis de plusieurs cellules d'une des feuilles de mon classeur. j'ai fais des test avec des formule "if" et "case" mais je reçois toujours un message d'erreur. Avec les "if" j'ai le message d'erreur "Bloc if sans End if" et sous "case" j'ai le message attendu fin d'instruction. En fait, j'ai besoin des résultats précis des cellules "c78","c44","c58" et "c50" de la feuille "Calcule" de mon classeur pour active la bonne macro. voici mon essaie "if". je suis vraiment rouillé en VBA cela fait une dizaine d'années que je n'y ai pas touché.

    Merci de votre aide

    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
    Sub Park()
    '
    ' park Macro
     
    If Range("c78") = "6" And Range("C44") = "8" And Range("C58") = "2" And Range("C50") = "2" Then
        macro6822
    If Range("c78") = "7" And Range("C44") = "7" And Range("C58")= "2" And Range("C50") = "2" Then
        macro7722
    If Range("c78") = "7" And Range("C44") = "8" And Range("C58") = "1" And Range("C50") = "2" Then
        macro7812
    If Range("c78") = "7" And Range("C44") = "8" And Range("C58") = "2" And Range("C50") = "1" Then
        macro7821
    If Range("c78") = "8" And Range("C44") = "6" And Range("C58") = "2" And Range("C50") = "2" Then
        macro8622
    If Range("c78") = "8" And Range("C44") = "7" And Range("C58") = "1" And Range("C50") = "2" Then
        macro8712
    If Range("c78") = "8" And Range("C44") = "7" And Range("C58") = "2" And Range("C50") = "1" Then
        macro8721
    If Range("c78") = "8" And Range("C44") = "8" And Range("C58") = "0" And Range("C50") = "2" Then
        macro8802
    If Range("c78") = "8" And Range("C44") = "8" And Range("C58") = "1" And Range("C50") = "1" Then
        macro8811
    If Range("c78") = "8" And Range("C44") = "8" And Range("C58") = "2" And Range("C50") = "0" Then
        macro8820
    If Range("c78") = "9" And Range("C44") = "5" And Range("C58") = "2" And Range("C50") = "2" Then
        macro9522
    If Range("c78") = "9" And Range("C44") = "6" And Range("C58") = "1" And Range("C50") = "2" Then
        macro9612
    If Range("c78") = "9" And Range("C44") = "6" And Range("C58") = "2" And Range("C50") = "1" Then
        macro9621
    If Range("c78") = "9" And Range("C44") = "7" And Range("C58") = "0" And Range("C50") = "2" Then
        macro9702
    If Range("c78") = "9" And Range("C44") = "7" And Range("C58") = "1" And Range("C50") = "1" Then
        macro9711
    If Range("c78") = "9" And Range("C44") = "7" And Range("C58") = "2" And Range("C50") = "0" Then
        macro9720
    End If
     
     
    End Sub

  2. #2
    Expert confirmé
    Homme Profil pro
    Electrotechnicien
    Inscrit en
    Juillet 2016
    Messages
    3 241
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 71
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Electrotechnicien

    Informations forums :
    Inscription : Juillet 2016
    Messages : 3 241
    Par défaut
    Bonjour,

    Le END IF ne sert à rien, supprimez-le.

    Cdlt

  3. #3
    Membre confirmé
    Homme Profil pro
    Superviseur en transport
    Inscrit en
    Août 2019
    Messages
    104
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Superviseur en transport
    Secteur : Transports

    Informations forums :
    Inscription : Août 2019
    Messages : 104
    Par défaut
    Merci ARTURO83,

    Mais, j'ai toujours mon message d'erreur "Bloc if sans End if".

  4. #4
    Expert confirmé
    Homme Profil pro
    Electrotechnicien
    Inscrit en
    Juillet 2016
    Messages
    3 241
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 71
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Electrotechnicien

    Informations forums :
    Inscription : Juillet 2016
    Messages : 3 241
    Par défaut
    vous ne devez pas faire le renvoi à la ligne , écrivez comme ceci (pour chaque ligne)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    If Range("c78") = "6" And Range("C44") = "8" And Range("C58") = "2" And Range("C50") = "2" Then macro6822
    Cdlt

  5. #5
    Invité
    Invité(e)
    Par défaut
    Citation Envoyé par Miraie Voir le message

    Bonjour,

    Désolé, si j'arrive après la bataille.

    Dans le sens de la réponse d'ARTURO83 :

    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
     
    Sub Park()
    '
    ' park Macro
       With Sheets("Feuil1") ' A adapter
            If .Range("C78") = "6" And .Range("C44") = "8" And .Range("C58").Value = "2" And .Range("C50") = "2" Then macro6822
            If .Range("C78") = "7" And .Range("C44") = "7" And .Range("C58").Value = "2" And .Range("C50") = "2" Then macro7722
            If .Range("C78") = "7" And .Range("C44") = "8" And .Range("C58").Value = "1" And .Range("C50") = "2" Then macro7812
            If .Range("C78") = "7" And .Range("C44") = "8" And .Range("C58").Value = "2" And .Range("C50") = "1" Then macro7821
            If .Range("C78") = "8" And .Range("C44") = "6" And .Range("C58").Value = "2" And .Range("C50") = "2" Then macro8622
            If .Range("C78") = "8" And .Range("C44") = "7" And .Range("C58").Value = "1" And .Range("C50") = "2" Then macro8712
            If .Range("C78") = "8" And .Range("C44") = "7" And .Range("C58").Value = "2" And .Range("C50") = "1" Then macro8721
            If .Range("C78") = "8" And .Range("C44") = "8" And .Range("C58").Value = "0" And .Range("C50") = "2" Then macro8802
            If .Range("C78") = "8" And .Range("C44") = "8" And .Range("C58").Value = "1" And .Range("C50") = "1" Then macro8811
            If .Range("C78") = "8" And .Range("C44") = "8" And .Range("C58").Value = "2" And .Range("C50") = "0" Then macro8820
            If .Range("C78") = "9" And .Range("C44") = "5" And .Range("C58").Value = "2" And .Range("C50") = "2" Then macro9522
            If .Range("C78") = "9" And .Range("C44") = "6" And .Range("C58").Value = "1" And .Range("C50") = "2" Then macro9612
            If .Range("C78") = "9" And .Range("C44") = "6" And .Range("C58").Value = "2" And .Range("C50") = "1" Then macro9621
            If .Range("C78") = "9" And .Range("C44") = "7" And .Range("C58").Value = "0" And .Range("C50") = "2" Then macro9702
            If .Range("C78") = "9" And .Range("C44") = "7" And .Range("C58").Value = "1" And .Range("C50") = "1" Then macro9711
            If .Range("C78") = "9" And .Range("C44") = "7" And .Range("C58").Value = "2" And .Range("C50") = "0" Then macro9720
      End With
     
    End Sub
    Sinon, une solution plus évolutive basée sur la recherche de la macro dans le projet d'après la solution présente dans le tuto de SilkyRoad
    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
    Sub Park2()
     
    Dim NomDeLaMacro As String
     
       With Sheets("Feuil1") ' A adapter
            NomDeLaMacro = "macro" & .Range("C78") & .Range("C44") & .Range("C58") & .Range("C50")
            Debug.Print NomDeLaMacro
            If PresenceMacro(ActiveWorkbook, NomDeLaMacro) = True Then Run NomDeLaMacro
      End With
     
    End Sub
     
     
    Function PresenceMacro(ByVal Wb As Workbook, ByVal MaMacro As String) As Boolean
     
    'Adapatation issue du tuto de SilkyRoad
    'Nécéssite d'activer la référence "Microsoft Visual basic For Application Extensibility 5.3"
     
    Dim I As Integer, x As Integer ', Ajout As Integer
    Dim VBCmp As VBComponent
    Dim Msg As String, ChaineATrouver As String
     
        PresenceMacro = False
        ChaineATrouver = MaMacro & "("
        For Each VBCmp In Wb.VBProject.VBComponents
     
             Msg = VBCmp.Name
             x = Wb.VBProject.VBComponents(Msg).CodeModule.CountOfLines
             For I = 1 To x
               With Wb.VBProject.VBComponents(Msg)
                    If InStr(1, .CodeModule.Lines(I, 1), ChaineATrouver, vbTextCompare) > 0 Then
                       PresenceMacro = True
                       Exit Function
                    End If
               End With
             Next
        Next VBCmp
     
    End Function

  6. #6
    Expert éminent Avatar de Menhir
    Homme Profil pro
    Ingénieur
    Inscrit en
    Juin 2007
    Messages
    16 037
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Finistère (Bretagne)

    Informations professionnelles :
    Activité : Ingénieur
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2007
    Messages : 16 037
    Par défaut
    Une petite variante :


    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
     
    Sub Park()
        Dim Cr As String
        Cr = Range("C78").Text & Range("C44").Text & Range("C58").Text & Range("C50")
     
        Select Case Cr
        Case "6822" : macro6822
        Case "7722" : macro7722
        Case "7812" : macro7812
        Case "7821" : macro7821
        Case "8622" : macro8622
        Case "8712" : macro8712
        Case "8721" : macro8721
        Case "8802" : macro8802
        Case "8811" : macro8811
        Case "8820" : macro8820
        Case "9522" : macro9522
        Case "9612" : macro9612
        Case "9621" : macro9621
        Case "9702" : macro9702
        Case "9711" : macro9711
        Case "9720" : macro9720
        End Select
     
    End Sub

  7. #7
    Membre confirmé
    Homme Profil pro
    Superviseur en transport
    Inscrit en
    Août 2019
    Messages
    104
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Superviseur en transport
    Secteur : Transports

    Informations forums :
    Inscription : Août 2019
    Messages : 104
    Par défaut
    Un énorme merci à tous! Vous m'avez rafraichit la mémoire et même appris de nouveaux codes. Vous êtes géniaux!

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

Discussions similaires

  1. Réponses: 1
    Dernier message: 29/05/2016, 18h30
  2. [XL-2003] VBA difference d'execution de macro selon si on est en pas à pas
    Par sharox dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 28/04/2015, 15h04
  3. [Toutes versions] Différent résultat de macro selon la ligne de tableau ?
    Par Invité dans le forum Macros et VBA Excel
    Réponses: 9
    Dernier message: 02/03/2015, 10h29
  4. [XL-2010] Executer une macro sur plusieurs cellules
    Par niconiko dans le forum Excel
    Réponses: 10
    Dernier message: 22/09/2014, 10h48
  5. Réponses: 5
    Dernier message: 15/12/2009, 15h28

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