Modifier la plage des cellules traitées dans une macro existante

Chri8Ed

XLDnaute Occasionnel
Bonjour à tous

J'ai besoin de modifier une macro
Celle-ci traite de l'ensemble d'une feuille
Je souhaiterais la restreindre sur la plage (J4:V(jusqu'à la dernière ligne))

Celle-ci se compose d'une boucle FOR EACH
Et je n'arrive pas délimiter la plage

Je pensais tout d'abord modifier la ligne suivante :
"For Each MonCommentaire In Sheets("C,car").Comments"
"For Each MonCommentaire In Sheets("C,car").Range"J4:V###").Comments"
Mais ce code n'est pas possible

J'avais pensé ensuite à utiliser une variable Range
et utiliser Set pour celle variable (Que je maitrise vraiment pas bien)
Mais je n'y arrive pas

Bref j'ai besoin d'aide

Merci d'avance

Code:
Sub Commentaire_Redimensionnement_Repositionnement()

Dim MonCommentaire As Comment

    For Each MonCommentaire In Sheets("C,car").Comments
        With MonCommentaire.Shape
        .Width = 50
        .Height = 35
    End With
    Next

  For Each c In ActiveSheet.Comments
    c.Shape.Left = c.Parent.Left + 20
    c.Shape.Top = c.Parent.Top - 20
  Next c
 
End Sub
 

pierrejean

XLDnaute Barbatruc
Bonjour Chri8Ed

A tester

Code:
Sub test()
For Each MonCommentaire In Sheets("C,car").Range("J4:V" & Sheets("C,car").Range("J" & Rows.Count).End(xlUp).Row).Comments
        With MonCommentaire.Shape
        .Width = 50
        .Height = 35
       End With
     End If
Next
For Each c In Sheets("C,car").Range("J4:V" & Sheets("C,car").Range("J" & Rows.Count).End(xlUp).Row).Comments
    c.Shape.Left = c.Parent.Left + 20
    c.Shape.Top = c.Parent.Top - 20
  Next c
End Sub
 

Chri8Ed

XLDnaute Occasionnel
Bonjour Pierrejean
Merci tout d'abord pour ton aide

Mais j'ai une erreur d’exécution '438'
"Propriété ou méthode non gérée par cet objet"

De plus je vois qu'il y a "End if" dans ton code, alors qu'il n'y a pas de If ?
Manque t-il une ligne ?

A+
 

pierrejean

XLDnaute Barbatruc
Re
Exact ,le End if est bien en trop
tester en le supprimant
Si echec tenter avec le code suivant et si toujours pas de succès poster un fichier exemple

Code:
Sub test()
 For Each MonCommentaire In Sheets("C,car").Comments
 If Not Intersect(MonCommentaire.Parent, Sheets("C,car").Range("J4:V" & Sheets("C,car").Range("J" & Rows.Count).End(xlUp).Row)) Is Nothing Then
        With MonCommentaire.Shape
        .Width = 50
        .Height = 35
       End With
     End If
Next
For Each c In Sheets("C,car").Range("J4:V" & Sheets("C,car").Range("J" & Rows.Count).End(xlUp).Row).Comments
    c.Shape.Left = c.Parent.Left + 20
    c.Shape.Top = c.Parent.Top - 20
  Next c
End Sub
 

Chri8Ed

XLDnaute Occasionnel
Re,
J'avais déjà supprimé le "End if", mais sans succès
Avec ce nouveau code, le 1er For Each passe sans problème
Mais plante au 2ème
Ce qui semble logique
J'ai donc modifier le code ainsi
Et tout fonctionne parfaitement

Il semble donc bien que avec For Each, il n'est pas possible de faire référence directement à une plage
Je vais donc utiliser cette méthode pour les prochaines fois

Merci encore pour ton aide
A+

VB:
Sub testComm2()

For Each MonCommentaire In Sheets("C,car").Comments
If Not Intersect(MonCommentaire.Parent, Sheets("C,car").Range("J4:V" & Sheets("C,car") _
.Range("J" & Rows.Count).End(xlUp).Row)) Is Nothing Then
        With MonCommentaire.Shape
        .Width = 50
        .Height = 35
       End With
     End If
Next

For Each c In Sheets("C,car").Comments
If Not Intersect(c.Parent, Sheets("C,car").Range("J4:V" & Sheets("C,car") _
.Range("J" & Rows.Count).End(xlUp).Row)) Is Nothing Then
    With c.Shape
    c.Shape.Left = c.Parent.Left + 20
    c.Shape.Top = c.Parent.Top - 20
    End With
End If
Next c
End Sub
 

Discussions similaires