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 :

Ajouter une "Target" à Private Sub Worksheet_Change(ByVal Target As Range) [XL-2007]


Sujet :

Macros et VBA Excel

  1. #1
    Membre à l'essai
    Homme Profil pro
    Rédacteur technique
    Inscrit en
    Décembre 2014
    Messages
    27
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Rédacteur technique
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2014
    Messages : 27
    Points : 10
    Points
    10
    Par défaut Ajouter une "Target" à Private Sub Worksheet_Change(ByVal Target As Range)
    Bonjour à toutes et à tous,

    Je suis débutant, je travaille avec une macro qui toune à merveille, j'aimerai simplement ajouter en copie la feuille2 de cette macro puis une "Target"en "AF"et copier ma nouvelle sélection en feuille3.
    et vérouiller par le même mot de passe mes feuilles (1,3, base). mais c'est pas facile pour un débutant.

    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
    Private Sub Worksheet_Change(ByVal Target As Range)
            If Intersect(Target, Columns("AE")) Is Nothing Or Target.Cells.Count > 1 Then Exit Sub
            ActiveSheet.Unprotect Password:="TEST"
            Dim sheetTemp As Worksheet
            Dim sheetToPaste As Worksheet
            Dim rng As Range
     
            If Target <> "" Then
                Range("J" & Target.Row).Value = Now()
                Application.Union(Range("A" & Target.Row & ":E" & Target.Row), Range("J" & Target.Row & ":J" & Target.Row), Range("X" & Target.Row & ":X" & Target.Row), Range("AD" & Target.Row & ":AE" & Target.Row), Range("AG" & Target.Row & ":AN" & Target.Row)).SpecialCells(xlCellTypeVisible).Select
                Selection.Copy
                Set sheetTemp = ActiveSheet
                Set sheetToPaste = Worksheets("Feuil1")
                sheetToPaste.Activate
                lastRow = sheetToPaste.Columns(1).Find(What:="*", SearchDirection:=xlPrevious).Row
                sheetToPaste.Range("A" & lastRow + 1).Select
                Selection.PasteSpecial Paste:=xlPasteValues
                Set rng = sheetToPaste.Range("A2", "Q" & lastRow + 2)
                rng.RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17), Header:=xlNo
                sheetTemp.Activate
                        Range("a" & Target.Row).Resize(1, 40).Locked = True
            Else
                Range("a" & Target.Row).Resize(1, 40).Locked = False
     
     
            End If
                ActiveSheet.Protect Password:="TEST"
     
     
    End Sub
    Merci de votre précieuse aide

  2. #2
    Expert éminent sénior
    Homme Profil pro
    aucune
    Inscrit en
    Septembre 2011
    Messages
    8 202
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : aucune

    Informations forums :
    Inscription : Septembre 2011
    Messages : 8 202
    Points : 14 353
    Points
    14 353
    Par défaut
    Bonjour,

    Pas sûr d'avoir bien compris. Ton code pourrait s'articuler comme suit :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 31 And Target.Cells.Count = 1 Then '(Target.Count = 1 le fait aussi)
        'ton code
    ElseIf Target.Column = 32 And Target.Cells.Count = 1 Then
        'ton code
    End If
    et, pour les mots de passe :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Sheets("1").Protect Password:="TEST"
    Sheets("3").Protect Password:="TEST"
    Sheets("base").Protect Password:="TEST"
    Cordialement.

    Daniel

    La plus perdue de toutes les journées est celle où l'on n'a pas ri. Chamfort

  3. #3
    Membre à l'essai
    Homme Profil pro
    Rédacteur technique
    Inscrit en
    Décembre 2014
    Messages
    27
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Rédacteur technique
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2014
    Messages : 27
    Points : 10
    Points
    10
    Par défaut
    Re bonjour Daniel,

    j'ai malheureusement erreur de compilation "Else sans IF"

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ElseIf Target.Column = 32 And Target.Cells.Count = 1 Then.
    J'espère avoir bien compris , ci joint le code mis à jour.

    Bloc note.txt

    Cdt

  4. #4
    Expert éminent sénior
    Homme Profil pro
    aucune
    Inscrit en
    Septembre 2011
    Messages
    8 202
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : aucune

    Informations forums :
    Inscription : Septembre 2011
    Messages : 8 202
    Points : 14 353
    Points
    14 353
    Par défaut
    Oui ça a l'air d'être bon.
    Cordialement.

    Daniel

    La plus perdue de toutes les journées est celle où l'on n'a pas ri. Chamfort

  5. #5
    Membre à l'essai
    Homme Profil pro
    Rédacteur technique
    Inscrit en
    Décembre 2014
    Messages
    27
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Rédacteur technique
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2014
    Messages : 27
    Points : 10
    Points
    10
    Par défaut
    Bien malheureusement erreur de compilation "Else sans IF", sur la seconde partie du code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ElseIf Target.Column = 32 And Target.Cells.Count = 1 Then

  6. #6
    Expert éminent sénior
    Homme Profil pro
    aucune
    Inscrit en
    Septembre 2011
    Messages
    8 202
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : aucune

    Informations forums :
    Inscription : Septembre 2011
    Messages : 8 202
    Points : 14 353
    Points
    14 353
    Par défaut
    Il manque un "End If" avant le ElseIf :

    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
        If Target <> "" Then
            Range("J" & Target.Row).Value = Now()
            Application.Union(Range("A" & Target.Row & ":E" & Target.Row), Range("J" & Target.Row & ":J" & Target.Row), Range("X" & Target.Row & ":X" & Target.Row), Range("AD" & Target.Row & ":AE" & Target.Row), Range("AG" & Target.Row & ":AN" & Target.Row)).SpecialCells(xlCellTypeVisible).Select
            Selection.Copy
            Set sheetTemp = ActiveSheet
            Set sheetToPaste = Worksheets("Feuil1")
            sheetToPaste.Activate
            lastRow = sheetToPaste.Columns(1).Find(What:="*", SearchDirection:=xlPrevious).Row
            sheetToPaste.Range("A" & lastRow + 1).Select
            Selection.PasteSpecial Paste:=xlPasteValues
            Set rng = sheetToPaste.Range("A2", "Q" & lastRow + 2)
            rng.RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17), Header:=xlNo
            sheetTemp.Activate
            Range("a" & Target.Row).Resize(1, 40).Locked = True
         Else
         Range("a" & Target.Row).Resize(1, 40).Locked = False
    '******************** ICI***************************************
    ElseIf Target.Column = 32 And Target.Cells.Count = 1 Then
    Cordialement.

    Daniel

    La plus perdue de toutes les journées est celle où l'on n'a pas ri. Chamfort

  7. #7
    Membre à l'essai
    Homme Profil pro
    Rédacteur technique
    Inscrit en
    Décembre 2014
    Messages
    27
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Rédacteur technique
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2014
    Messages : 27
    Points : 10
    Points
    10
    Par défaut
    J'ai corrigé et maintenant j'ai Erreur de compilation:

    Déclaration existante dans la portée en cours:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    ElseIf Target.Column = 32 And Target.Cells.Count = 1 Then
    ActiveSheet.Unprotect Password:="TEST"
     Dim sheetTemp As Worksheet


    j'espère ne pas avoir fait d'erreur cette fois

    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
    Private Sub Worksheet_Change(ByVal Target As Range)
      
     If Target.Column = 31 And Target.Cells.Count = 1 Then '(Target.Count = 1 le fait aussi)
     ActiveSheet.Unprotect Password:="TEST"
     Dim sheetTemp As Worksheet
     Dim sheetToPaste As Worksheet
     Dim sheetToPaste2 As Worksheet
     Dim rng As Range
     
    If Target <> "" Then
     Range("J" & Target.Row).Value = Now()
     Application.Union(Range("A" & Target.Row & ":E" & Target.Row), Range("J" & Target.Row & ":J" & Target.Row), Range("X" & Target.Row & ":X" & Target.Row), Range("AD" & Target.Row & ":AE" & Target.Row), Range("AG" & Target.Row & ":AN" & Target.Row)).SpecialCells(xlCellTypeVisible).Select
     Selection.Copy
     Set sheetTemp = ActiveSheet
     Set sheetToPaste = Worksheets("Feuil1")
     sheetToPaste.Activate
     lastRow = sheetToPaste.Columns(1).Find(What:="*", SearchDirection:=xlPrevious).Row
     sheetToPaste.Range("A" & lastRow + 1).Select
     Selection.PasteSpecial Paste:=xlPasteValues
     Set rng = sheetToPaste.Range("A2", "Q" & lastRow + 2)
     rng.RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17), Header:=xlNo
     sheetTemp.Activate
     Range("a" & Target.Row).Resize(1, 40).Locked = True
     Else
     Range("a" & Target.Row).Resize(1, 40).Locked = False
    
    End If
    
    ElseIf Target.Column = 32 And Target.Cells.Count = 1 Then
    ActiveSheet.Unprotect Password:="TEST"
     Dim sheetTemp As Worksheet
     Dim sheetToPaste As Worksheet
     Dim sheetToPaste2 As Worksheet
     Dim rng As Range
     
    If Target <> "" Then
     Range("J" & Target.Row).Value = Now()
     Application.Union(Range("A" & Target.Row & ":E" & Target.Row), Range("J" & Target.Row & ":J" & Target.Row), Range("X" & Target.Row & ":X" & Target.Row), Range("AD" & Target.Row & ":AE" & Target.Row), Range("AG" & Target.Row & ":AN" & Target.Row)).SpecialCells(xlCellTypeVisible).Select
     Selection.Copy
     Set sheetTemp = ActiveSheet
     Set sheetToPaste = Worksheets("Feuil2")
     sheetToPaste.Activate
     lastRow = sheetToPaste.Columns(1).Find(What:="*", SearchDirection:=xlPrevious).Row
     sheetToPaste.Range("A" & lastRow + 1).Select
     Selection.PasteSpecial Paste:=xlPasteValues
     Set rng = sheetToPaste.Range("A2", "Q" & lastRow + 2)
     rng.RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17), Header:=xlNo
     sheetTemp.Activate
     Range("a" & Target.Row).Resize(1, 40).Locked = True
     Else
     Range("a" & Target.Row).Resize(1, 40).Locked = False
    
      End If
     Sheets("Base").Protect Password:="TEST"
    Sheets("Feuil1").Protect Password:="TEST"
    Sheets("Feuil2").Protect Password:="TEST"
    
    End Sub
    Cdt

  8. #8
    Expert éminent sénior
    Homme Profil pro
    aucune
    Inscrit en
    Septembre 2011
    Messages
    8 202
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : aucune

    Informations forums :
    Inscription : Septembre 2011
    Messages : 8 202
    Points : 14 353
    Points
    14 353
    Par défaut
    Déclaration existante dans la portée en cours:
    C'est du langage Microsoft pour dire que tu déclares plusieurs fois la même variable.
    Il ne faut pas que tu répètes tes déclarations (Dim...). Tu ne peux les faire qu'une fois par macro. C'est d'ailleurs suffisant.
    Cordialement.

    Daniel

    La plus perdue de toutes les journées est celle où l'on n'a pas ri. Chamfort

  9. #9
    Membre à l'essai
    Homme Profil pro
    Rédacteur technique
    Inscrit en
    Décembre 2014
    Messages
    27
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Rédacteur technique
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2014
    Messages : 27
    Points : 10
    Points
    10
    Par défaut
    Donc je supprime DIM de la seconde partie du code après :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ElseIf Target.Column = 32 And Target.Cells.Count = 1 Then
    Pas façile d'avoir affaire à un débutant désolé

  10. #10
    Expert éminent sénior
    Homme Profil pro
    aucune
    Inscrit en
    Septembre 2011
    Messages
    8 202
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : aucune

    Informations forums :
    Inscription : Septembre 2011
    Messages : 8 202
    Points : 14 353
    Points
    14 353
    Par défaut
    Donc je supprime DIM de la seconde partie du code après :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ElseIf Target.Column = 32 And Target.Cells.Count = 1 Then
    Tout à fait.

    Pas façile d'avoir affaire à un débutant
    "L'homme arrive novice à chaque âge de la vie."

    Chamfort

    Donc, pas de problème, on est tous passé par là. ;
    Cordialement.

    Daniel

    La plus perdue de toutes les journées est celle où l'on n'a pas ri. Chamfort

  11. #11
    Membre à l'essai
    Homme Profil pro
    Rédacteur technique
    Inscrit en
    Décembre 2014
    Messages
    27
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Rédacteur technique
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2014
    Messages : 27
    Points : 10
    Points
    10
    Par défaut
    La première partie du script fonctionne parfaitement , mais la seconde j'ai enlevé DIM répétés et lorsque je sélectionne ma liste déroulante colonne"32" message erreur d'exécution '91': Variable objet ou variable de bloc With non défini. pour
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    lastRow = sheetToPaste.Columns(1).Find(What:="*", SearchDirection:=xlPrevious).Row
    Ci-joint le code modifié:
    CdtBloc note.txt

  12. #12
    Expert éminent sénior
    Homme Profil pro
    aucune
    Inscrit en
    Septembre 2011
    Messages
    8 202
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : aucune

    Informations forums :
    Inscription : Septembre 2011
    Messages : 8 202
    Points : 14 353
    Points
    14 353
    Par défaut
    Comme le message ne l'indique pas, la colonne doit être vide.
    Cordialement.

    Daniel

    La plus perdue de toutes les journées est celle où l'on n'a pas ri. Chamfort

  13. #13
    Membre à l'essai
    Homme Profil pro
    Rédacteur technique
    Inscrit en
    Décembre 2014
    Messages
    27
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Rédacteur technique
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2014
    Messages : 27
    Points : 10
    Points
    10
    Par défaut
    Vraiment merci beaucoup pour votre patience et votre dévouement.

    Alléluia!!!
    Finalement en mettant un titre à chaque colonne de ma feuille 2 cela fonctionne, il ne me reste plus qu'à finaliser les mots de passe qui eux me donnent du fil à retordre. erreur 9 : l'indice n'appartient pas à la sélection.
    Je suis à la bourre!!!^^

  14. #14
    Expert éminent sénior
    Homme Profil pro
    aucune
    Inscrit en
    Septembre 2011
    Messages
    8 202
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : aucune

    Informations forums :
    Inscription : Septembre 2011
    Messages : 8 202
    Points : 14 353
    Points
    14 353
    Par défaut
    erreur 9 : l'indice n'appartient pas à la sélection
    Ca veut dire que le nom de la feuille n'est pas le bon.
    Cordialement.

    Daniel

    La plus perdue de toutes les journées est celle où l'on n'a pas ri. Chamfort

  15. #15
    Membre à l'essai
    Homme Profil pro
    Rédacteur technique
    Inscrit en
    Décembre 2014
    Messages
    27
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Rédacteur technique
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2014
    Messages : 27
    Points : 10
    Points
    10
    Par défaut
    Bonjour Daniel,

    J'ai bien essayé de placer à différents endroits:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ActiveSheet.Unprotect Password:="TEST"
    et
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Sheets("1").Protect Password:="TEST"
    Sheets("2").Protect Password:="TEST"
    Sheets("base").Protect Password:="TEST"
    la seule solution est de désactiver la protection de la feuille, cela fonctionne , mais je peux alors modifier toutes mes cellules.

    Mais impossible au final d'avoir toutes mes feuilles verrouillées (pour ne pas "polluer" mes informations).
    et pour résoudre mon classeur il me manque aussi la copie de la feuille1 dans une autre feuille (un collé identique de la feuille1)
    Pour le reste tout va bien.
    Cdt
    Bloc Note.txt

  16. #16
    Expert éminent sénior
    Homme Profil pro
    aucune
    Inscrit en
    Septembre 2011
    Messages
    8 202
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : aucune

    Informations forums :
    Inscription : Septembre 2011
    Messages : 8 202
    Points : 14 353
    Points
    14 353
    Par défaut
    Bonjour,

    J'ai bien essayé de placer à différents endroits:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ActiveSheet.Unprotect Password:="TEST"
    et
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Sheets("1").Protect Password:="TEST"
    Sheets("2").Protect Password:="TEST"
    Sheets("base").Protect Password:="TEST"
    la seule solution est de désactiver la protection de la feuille, cela fonctionne , mais je peux alors modifier toutes mes cellules.
    Tu les déprotèges juste le temps de l'exécution de la macro et tu les reprotèges ensuite. Sinon, tu peux les protéger comme ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Sheets("1").Protect Password:="TEST", Userinterfaceonly=True
    Cordialement.

    Daniel

    La plus perdue de toutes les journées est celle où l'on n'a pas ri. Chamfort

  17. #17
    Membre à l'essai
    Homme Profil pro
    Rédacteur technique
    Inscrit en
    Décembre 2014
    Messages
    27
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Rédacteur technique
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2014
    Messages : 27
    Points : 10
    Points
    10
    Par défaut
    Re Bonjour Daniel,

    j'ai le code erreu suivant : Erreur de compilation: attendu: paramètre nommé
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Sheets("2N Traité").Protect Password:="TEST", Userinterfaceonly=True
    décidément je vais pas pouvoir me proèger...

    à l'heure actuelle je ne sais plus ou placer mes (ActiveSheet.protect Password:="TEST") pour que ça fonctionne correctement comme avec ce code actuel
    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
    Private Sub Worksheet_Change(ByVal Target As Range)
     
     If Target.Column = 31 And Target.Cells.Count = 1 Then '(Target.Count = 1 le fait aussi)
     ActiveSheet.Unprotect Password:="TEST"
     Dim sheetTemp As Worksheet
     Dim sheetToPaste As Worksheet
     Dim sheetToPaste2 As Worksheet
     Dim rng As Range
     
    If Target <> "" Then
     Range("J" & Target.Row).Value = Now()
     Application.Union(Range("A" & Target.Row & ":E" & Target.Row), Range("J" & Target.Row & ":J" & Target.Row), Range("X" & Target.Row & ":X" & Target.Row), Range("AD" & Target.Row & ":AE" & Target.Row), Range("AG" & Target.Row & ":AN" & Target.Row)).SpecialCells(xlCellTypeVisible).Select
     Selection.Copy
     Set sheetTemp = ActiveSheet
     Set sheetToPaste = Worksheets("2N Traité")
     sheetToPaste.Activate
     lastRow = sheetToPaste.Columns(1).Find(What:="*", SearchDirection:=xlPrevious).Row
     sheetToPaste.Range("A" & lastRow + 1).Select
     Selection.PasteSpecial Paste:=xlPasteValues
     Set rng = sheetToPaste.Range("A2", "Q" & lastRow + 2)
     rng.RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17), Header:=xlNo
     sheetTemp.Activate
     Range("a" & Target.Row).Resize(1, 40).Locked = True
     Else
     Range("a" & Target.Row).Resize(1, 40).Locked = False
     
    End If
     
     
    ElseIf Target.Column = 32 And Target.Cells.Count = 1 Then
    ActiveSheet.Unprotect Password:="TEST"
     
    If Target <> "" Then
     Range("J" & Target.Row).Value = Now()
     Application.Union(Range("A" & Target.Row & ":E" & Target.Row), Range("J" & Target.Row & ":J" & Target.Row), Range("X" & Target.Row & ":X" & Target.Row), Range("AD" & Target.Row & ":AE" & Target.Row), Range("AG" & Target.Row & ":AN" & Target.Row)).SpecialCells(xlCellTypeVisible).Select
     Selection.Copy
     Set sheetTemp = ActiveSheet
     Set sheetToPaste = Worksheets("Dossiers Manquants")
     sheetToPaste.Activate
     lastRow = sheetToPaste.Columns(1).Find(What:="*", SearchDirection:=xlPrevious).Row
     sheetToPaste.Range("A" & lastRow + 1).Select
     Selection.PasteSpecial Paste:=xlPasteValues
     Set rng = sheetToPaste.Range("A2", "H" & lastRow + 2)
     rng.RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6, 7, 8), Header:=xlNo
     sheetTemp.Activate
     Range("a" & Target.Row).Resize(1, 40).Locked = True
     Else
     Range("a" & Target.Row).Resize(1, 40).Locked = False
     
    End If
     
    End If
    Sheets("2N Traité").Protect Password:="TEST", Userinterfaceonly=True
    End Sub
    De plus j'aimerai coller sur une autre feuille la macro ("2Ntraité"),j'ai essayé de recopier le code mais erreur, j'ai essayé d'intégrer une seconde
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Set sheetToPaste = Worksheets("Feuil3")
    sans succés. heureusement que le ciel est gris pour rester concentré.

  18. #18
    Expert éminent sénior
    Homme Profil pro
    aucune
    Inscrit en
    Septembre 2011
    Messages
    8 202
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : aucune

    Informations forums :
    Inscription : Septembre 2011
    Messages : 8 202
    Points : 14 353
    Points
    14 353
    Par défaut
    Désolé, la bonne syntaxe est :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Sheets("2N Traité").Protect Password:="TEST", Userinterfaceonly:=True
    Dans le module "ThisWorkbook", tu ôtes la protectiion de tes feuilles, uniquement pour les macros avec :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    Private Sub Workbook_Open()
        Sheets("2N Traité").Protect Password:="TEST", Userinterfaceonly:=True
        ' pareil pour les autres feuilles
    End Sub
    Après, tu n'as plus à te soucier de déprotéger ou protéger tes feuilles.

    De plus j'aimerai coller sur une autre feuille la macro ("2Ntraité"),j'ai essayé de recopier le code mais erreur, j'ai essayé d'intégrer une seconde
    Poste ton code.
    Cordialement.

    Daniel

    La plus perdue de toutes les journées est celle où l'on n'a pas ri. Chamfort

  19. #19
    Membre à l'essai
    Homme Profil pro
    Rédacteur technique
    Inscrit en
    Décembre 2014
    Messages
    27
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Rédacteur technique
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2014
    Messages : 27
    Points : 10
    Points
    10
    Par défaut
    Re bonjour Daniel,

    Génial ça fonctionne enfin très bien avec ce nouveau code merci.
    Je vois le bout du tunel...
    Il ne reste plus qu'à savoir comment coller sur une seconde feuille, le code identique
    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
    Private Sub Worksheet_Change(ByVal Target As Range)
     
     If Target.Column = 31 And Target.Cells.Count = 1 Then '(Target.Count = 1 le fait aussi)
     ActiveSheet.Unprotect Password:="TEST"
     Dim sheetTemp As Worksheet
     Dim sheetToPaste As Worksheet
     Dim sheetToPaste2 As Worksheet
     Dim rng As Range
     
    If Target <> "" Then
     Range("J" & Target.Row).Value = Now()
     Application.Union(Range("A" & Target.Row & ":E" & Target.Row), Range("J" & Target.Row & ":J" & Target.Row), Range("X" & Target.Row & ":X" & Target.Row), Range("AD" & Target.Row & ":AE" & Target.Row), Range("AG" & Target.Row & ":AN" & Target.Row)).SpecialCells(xlCellTypeVisible).Select
     Selection.Copy
     Set sheetTemp = ActiveSheet
     Set sheetToPaste = Worksheets("2N Traité")'''''''''& Worksheet("feuil3")''''''''???
     sheetToPaste.Activate
    Voici le code final qui fonctionne très bien grace à toi merci mille fois

    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
    Private Sub Worksheet_Change(ByVal Target As Range)
     
     If Target.Column = 31 And Target.Cells.Count = 1 Then '(Target.Count = 1 le fait aussi)
     ActiveSheet.Unprotect Password:="TEST"
     Dim sheetTemp As Worksheet
     Dim sheetToPaste As Worksheet
     Dim sheetToPaste2 As Worksheet
     Dim rng As Range
     
    If Target <> "" Then
     Range("J" & Target.Row).Value = Now()
     Application.Union(Range("A" & Target.Row & ":E" & Target.Row), Range("J" & Target.Row & ":J" & Target.Row), Range("X" & Target.Row & ":X" & Target.Row), Range("AD" & Target.Row & ":AE" & Target.Row), Range("AG" & Target.Row & ":AN" & Target.Row)).SpecialCells(xlCellTypeVisible).Select
     Selection.Copy
     Set sheetTemp = ActiveSheet
     Set sheetToPaste = Worksheets("2N Traité")
     sheetToPaste.Activate
     lastRow = sheetToPaste.Columns(1).Find(What:="*", SearchDirection:=xlPrevious).Row
     sheetToPaste.Range("A" & lastRow + 1).Select
     Selection.PasteSpecial Paste:=xlPasteValues
     Set rng = sheetToPaste.Range("A2", "Q" & lastRow + 2)
     rng.RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17), Header:=xlNo
     sheetTemp.Activate
     Range("a" & Target.Row).Resize(1, 40).Locked = True
     Else
     Range("a" & Target.Row).Resize(1, 40).Locked = False
     
    End If
     
    ElseIf Target.Column = 32 And Target.Cells.Count = 1 Then
    ActiveSheet.Unprotect Password:="TEST"
     
    If Target <> "" Then
     Range("J" & Target.Row).Value = Now()
     Application.Union(Range("A" & Target.Row & ":E" & Target.Row), Range("J" & Target.Row & ":J" & Target.Row), Range("X" & Target.Row & ":X" & Target.Row), Range("AD" & Target.Row & ":AE" & Target.Row), Range("AG" & Target.Row & ":AN" & Target.Row)).SpecialCells(xlCellTypeVisible).Select
     Selection.Copy
     Set sheetTemp = ActiveSheet
     Set sheetToPaste = Worksheets("Dossiers Manquants")
     sheetToPaste.Activate
     lastRow = sheetToPaste.Columns(1).Find(What:="*", SearchDirection:=xlPrevious).Row
     sheetToPaste.Range("A" & lastRow + 1).Select
     Selection.PasteSpecial Paste:=xlPasteValues
     Set rng = sheetToPaste.Range("A2", "H" & lastRow + 2)
     rng.RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6, 7, 8), Header:=xlNo
     sheetTemp.Activate
     Range("a" & Target.Row).Resize(1, 40).Locked = True
     Else
     Range("a" & Target.Row).Resize(1, 40).Locked = False
     
    End If
     
    End If
    Sheets("2N Traité").Protect Password:="TEST", Userinterfaceonly:=True
    Sheets("Dossiers Manquants").Protect Password:="TEST", Userinterfaceonly:=True
    Sheets("Base BI ").Protect Password:="TEST", Userinterfaceonly:=True
    End Sub

  20. #20
    Expert éminent sénior
    Homme Profil pro
    aucune
    Inscrit en
    Septembre 2011
    Messages
    8 202
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : aucune

    Informations forums :
    Inscription : Septembre 2011
    Messages : 8 202
    Points : 14 353
    Points
    14 353
    Par défaut
    Condition pour 1ère copie :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     If Target.Column = 31 And Target.Cells.Count = 1 Then '(Target.Count = 1 le fait aussi)
    Condition ppour deuxième copoie :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     ElseIf Target.Column = 32 And Target.Cells.Count = 1 Then
    Quelle est la condition pour la 3e copie ?
    Cordialement.

    Daniel

    La plus perdue de toutes les journées est celle où l'on n'a pas ri. Chamfort

+ Répondre à la discussion
Cette discussion est résolue.
Page 1 sur 2 12 DernièreDernière

Discussions similaires

  1. [Toutes versions] Private Sub Worksheet_Change(ByVal Target As Range) et protection
    Par Giantrick dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 07/01/2013, 10h21
  2. [XL-2007] petit soucis avec un Private Sub Worksheet_Change(ByVal Target As Range)
    Par dris974 dans le forum Macros et VBA Excel
    Réponses: 6
    Dernier message: 28/03/2011, 12h57

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