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. #21
    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
    Non en fait je voudrais simplement coller le code suivant en même temps sur les feuilles : ("2N traité") et ("feuille3")
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    comme un "collé à l'identique" sur deux feuilles différentes...

  2. #22
    Expert éminent sénior
    Homme Profil pro
    aucune
    Inscrit en
    Septembre 2011
    Messages
    8 203
    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 203
    Points : 14 354
    Points
    14 354
    Par défaut
    Non en fait je voudrais simplement coller feuille ("2N traité") et ("feuille3") comme un "collé à l'identique" sur deux feuilles différentes
    Ce n'est pas clair pour moi. Tu fais simplement une sélection de cellules visibles. Est-ce que tu veux les copier sur tes deux feuilles ? Et si oui, à quel endroit ? Au même emplacement ?
    Cordialement.

    Daniel

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

  3. #23
    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,

    Désolé pour hier soir j'ai du partir précipitamment.
    Alors je vais essayer d'être un peur plus clair aujourd'hui.
    Dans le code suivant j'aimerai ajouter à "Set sheetToPaste = Worksheets("2N Traité"),une seconde feuille "à coller"Worksheets(feuil2)"
    Afin d'avoir un second onglet identique à l'onglet "2N Traité"sans protection cette fois.
    Et la grace à toi j'aurai résolu ma demande.

    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
    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é")',Worksheets("feuil2") si possible
     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
    Encore merci de m'accorder du temps.

    Cordialement

    Christophe

  4. #24
    Expert éminent sénior
    Homme Profil pro
    aucune
    Inscrit en
    Septembre 2011
    Messages
    8 203
    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 203
    Points : 14 354
    Points
    14 354
    Par défaut
    Afin d'avoir un second onglet identique à l'onglet "2N Traité"sans protection cette fois.
    Pour ce qui est de la protection, reporte-toi au message #18. Cette macro s'exécute à l'ouverture, ainsi, la protection ne s'applique pas aux macros. Si tu n'en comprends pas la teneur, dis-le. Pour ce qui est des deux feuilles, une solution consiste à créer un array avec le nom des feuilles :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Feuilles = Array("2N Traité", "feuil2")
    et à faire une boucle sur chacune des feuilles :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    For Each Item In Feuilles
    '...
    Next Item
    Ton code modifié devient (non 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
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    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
     Dim Feuilles As Variant
     
    If Target <> "" Then
     Feuilles = Array("2N Traité", "feuil2")
     For Each Item In Feuilles
        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(Item) ',Worksheets("feuil2") si possible
        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
     Next Item
    End If
    Cordialement.

    Daniel

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

  5. #25
    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 mis à jour mon code et j'ai l'Erreur de compilation:"Else sans If" taggé sur Else
    du code.
    On y est presque...
    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 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
     Dim Feuilles As Variant
     
    If Target <> "" Then
     Feuilles = Array("2N Traité", "feuil2")
     For Each Item In Feuilles
        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(Item) ',Worksheets("feuil2") si possible
        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
     Next Item
    End If

  6. #26
    Expert éminent sénior
    Homme Profil pro
    aucune
    Inscrit en
    Septembre 2011
    Messages
    8 203
    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 203
    Points : 14 354
    Points
    14 354
    Par défaut
    Bonjour,

    A quel "If" se réfère ce Else" ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
        Range("a" & Target.Row).Resize(1, 40).Locked = True
        Else
        Range("a" & Target.Row).Resize(1, 40).Locked = False
    Cordialement.

    Daniel

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

  7. #27
    Expert éminent sénior
    Avatar de Marc-L
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Avril 2013
    Messages
    9 468
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2013
    Messages : 9 468
    Points : 18 677
    Points
    18 677
    Par défaut



    Bonjour, bonjour !

    C'est pourtant simple, du niveau de l'école élémentaire ‼

    Dans ton code, combien de lignes commençant par If et se terminant par Then ? Et combien de End If ?!
    C'est parce que la vitesse de la lumière est plus rapide que celle du son que tant de gens paressent brillants avant d'avoir l'air con ! (Thomas Boishardy)

  8. #28
    Expert éminent Avatar de casefayere
    Homme Profil pro
    RETRAITE
    Inscrit en
    Décembre 2006
    Messages
    5 138
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 69
    Localisation : France, Ardennes (Champagne Ardenne)

    Informations professionnelles :
    Activité : RETRAITE
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Décembre 2006
    Messages : 5 138
    Points : 9 548
    Points
    9 548
    Par défaut
    Bonjour,
    Dans ton code, combien de lignes commençant par If et se terminant par Then ? Et combien de End If ?!
    Pour moi, c'est une histoire de placement, ça ne serait pas (sans analyser le contenu)
    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
    If Target <> "" Then
     Feuilles = Array("2N Traité", "feuil2")
     For Each Item In Feuilles
        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(Item) ',Worksheets("feuil2") si possible
        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
     Next Item
        Else
        Range("a" & Target.Row).Resize(1, 40).Locked = False
    End If
    Cordialement,
    Dom
    _____________________________________________
    Vous êtes nouveau ? pour baliser votre code, cliquer sur cet exemple : Anomaly
    pensez à cliquer sur si votre problème l'est
    Par contre, il est désagréable de voir une discussion résolue sans message final du demandeur (satisfaction, désarroi, remerciement, conclusion...)

  9. #29
    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 Marc,

    Malheureusement je n'en suis qu'à l'élémentaire....

    Mais ce n'est que la première partie du code, le code complet est celui-ci: il doit y maquer une subtilité que je ne connais pas

    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
    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
     Dim Feuilles As Variant
     
    If Target <> "" Then
     Feuilles = Array("2N Traité", "feuil2")
     For Each Item In Feuilles
        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(Item) ',Worksheets("feuil2") si possible
        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
     Next Item
     
    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
    casefayere, merci de ton intervention

    Erreur d'exécution '91' : Variable objet ou variable de bloc With non définie

    taggé sur la partie en gras du code suivant:
    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
    If Target <> "" Then
     Feuilles = Array("2N Traité", "feuil2")
     For Each Item In Feuilles
        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(Item) ',Worksheets("feuil2") si possible
        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
        Next Item
        Else
        Range("a" & Target.Row).Resize(1, 40).Locked = False
    
    End If

  10. #30
    Expert éminent Avatar de casefayere
    Homme Profil pro
    RETRAITE
    Inscrit en
    Décembre 2006
    Messages
    5 138
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 69
    Localisation : France, Ardennes (Champagne Ardenne)

    Informations professionnelles :
    Activité : RETRAITE
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Décembre 2006
    Messages : 5 138
    Points : 9 548
    Points
    9 548
    Par défaut
    L'erreur n'est plus la même, donc mon placement est bon mais comme je ne me suis pas plongé dans ton histoire, le reste.......,

    Je ne peux pas à ce stade, m'intégrer au fond du problème

    Par hasard, est-ce que "lastRow" est bien déclaré et comment ?
    Cordialement,
    Dom
    _____________________________________________
    Vous êtes nouveau ? pour baliser votre code, cliquer sur cet exemple : Anomaly
    pensez à cliquer sur si votre problème l'est
    Par contre, il est désagréable de voir une discussion résolue sans message final du demandeur (satisfaction, désarroi, remerciement, conclusion...)

  11. #31
    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
    casefayere,
    je garde ton placemement, et Lastrow est décallé par le surlignage en gras.

  12. #32
    Expert éminent Avatar de casefayere
    Homme Profil pro
    RETRAITE
    Inscrit en
    Décembre 2006
    Messages
    5 138
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 69
    Localisation : France, Ardennes (Champagne Ardenne)

    Informations professionnelles :
    Activité : RETRAITE
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Décembre 2006
    Messages : 5 138
    Points : 9 548
    Points
    9 548
    Par défaut
    Ma question n'est pas celle-là, Est-ce que "lastRow" est bien déclaré ?, c'est à dire, en début de module ou ailleurs
    Cordialement,
    Dom
    _____________________________________________
    Vous êtes nouveau ? pour baliser votre code, cliquer sur cet exemple : Anomaly
    pensez à cliquer sur si votre problème l'est
    Par contre, il est désagréable de voir une discussion résolue sans message final du demandeur (satisfaction, désarroi, remerciement, conclusion...)

  13. #33
    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
    Casefayere,

    Ma feuille ne contient que ce code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    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
     Dim Feuilles As Variant
     
    If Target <> "" Then
     Feuilles = Array("2N Traité", "feuil2")
     For Each Item In Feuilles
    et n'est pas déclaré
    Cordialement
    Christophe

  14. #34
    Expert éminent Avatar de casefayere
    Homme Profil pro
    RETRAITE
    Inscrit en
    Décembre 2006
    Messages
    5 138
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 69
    Localisation : France, Ardennes (Champagne Ardenne)

    Informations professionnelles :
    Activité : RETRAITE
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Décembre 2006
    Messages : 5 138
    Points : 9 548
    Points
    9 548
    Par défaut
    n'est pas déclaré
    d'où l'erreur, alors déclares-le et renvoies le code complet car, comme je l'ai précisé, je n'ai pas suivi tous les fils
    Cordialement,
    Dom
    _____________________________________________
    Vous êtes nouveau ? pour baliser votre code, cliquer sur cet exemple : Anomaly
    pensez à cliquer sur si votre problème l'est
    Par contre, il est désagréable de voir une discussion résolue sans message final du demandeur (satisfaction, désarroi, remerciement, conclusion...)

  15. #35
    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
    Casefayere, voici le code complet

    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
     Dim Feuilles As Variant
     
    If Target <> "" Then
     Feuilles = Array("2N Traité", "feuil2")
     For Each Item In Feuilles
        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(Item) ',Worksheets("feuil2") si possible
        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
    Next Item
    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
    Cordialement

  16. #36
    Expert éminent Avatar de casefayere
    Homme Profil pro
    RETRAITE
    Inscrit en
    Décembre 2006
    Messages
    5 138
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 69
    Localisation : France, Ardennes (Champagne Ardenne)

    Informations professionnelles :
    Activité : RETRAITE
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Décembre 2006
    Messages : 5 138
    Points : 9 548
    Points
    9 548
    Par défaut
    voilà comment j'aurai rédigé le début, en déclarant lastrow en range
    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 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, lastRow As Range
     Dim Feuilles As Variant
     
    If Target <> "" Then
      Feuilles = Array("2N Traité", "feuil2")
      For Each Item In Feuilles
        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).Copy
        Set sheetTemp = ActiveSheet
        Set sheetToPaste = Worksheets(Item) ',Worksheets("feuil2") si possible
        sheetToPaste.Activate
        Set lastRow = sheetToPaste.Columns(1).Find(What:="*", SearchDirection:=xlPrevious)
        If Not lastRow Is Nothing Then
          sheetToPaste.Range("A" & lastRow.Row + 1).PasteSpecial Paste:=xlPasteValues
          Set rng = sheetToPaste.Range("A2", "Q" & lastRow.Row + 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.Range("a" & Target.Row).Resize(1, 40).Locked = True
        End If
      Next Item
    Else
        Range("a" & Target.Row).Resize(1, 40).Locked = False
    End If
    le reste du code est à adapter surtout quand on fait référence à lastrow (déclaré en "Range"), regardes bien ce qui change ci-dessus
    Cordialement,
    Dom
    _____________________________________________
    Vous êtes nouveau ? pour baliser votre code, cliquer sur cet exemple : Anomaly
    pensez à cliquer sur si votre problème l'est
    Par contre, il est désagréable de voir une discussion résolue sans message final du demandeur (satisfaction, désarroi, remerciement, conclusion...)

  17. #37
    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
    Casefayere,

    Tout d'abord merci à toi et félicitations, ma demande est résolue.
    Merci à Daniel.C qui m'a fait avancer à grands pas.

+ Répondre à la discussion
Cette discussion est résolue.
Page 2 sur 2 PremièrePremière 12

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