For each p in that column” instead of “for each p in activesheet.pictures
Hi there,
I’ve been a web developer for about 16 years, but a rookie in VBA. Oh well, I get around not too bad.
But… Something I’m wondering about… Here’s the thing :
I have a worksheet. In that worksheet, I have a column (the J one) which contains (or not) one small image in each cell of that J column (about 3000 rows in the array)
So, when I activate this worksheet, I run a macro (Worksheet_Activate)
That macro is supposed to loop through each cell of my J column to check if there is any picture in it. And if yes then do blablabla
So far, no problem.
So here’s my macro (which works perfectly) :
For Each c In Range("My J column")
MyCell = c.Cells().Address
For Each p In ActiveSheet.Pictures
If Not Intersect(p.TopLeftCell, Range(MyCell)) Is Nothing Then
Msgbox “There is a picture in that cell” (that’s just an example)
End If
Next p
Next k
But I am wondering… In many other cells of my worksheet, I also have many pictures (nothing to do with my macro). If I understand well that code (didn’t find any other), unless I’m wrong, it basically does the following thing :
For each cell of the J column do that :
For each picture in my worksheet (let’s say I have 500 small pictures in the whole worksheet)
If a picture is found in the cell then tell it to me
And then keep on doing that for the next picture you find…
if I understand well, that means that at that point the program goes to the next picture of the worksheet to test if its in the cell of the J column ???? which would be totally silly as I just want the program to check the pictures within the J column, not in every picture of the worksheet (loss of time and energy for nothing)
And then do the same for the next cell in the J colum (That makes sense ok)
Anyone can tell me if there’s any other solution or if I’m doing anything the wrong way ?
My macro works perfectly, it’s just that I find it silly to test every picture of the worksheet if I just need to loop through every picture in the J column.
The right code would be something like “for each p in that column” instead of “for each p in activesheet.pictures
The thing is that I’ve been googlising for days but didn’t find any other solution
Thanks in advance
PS: Désolé, je pensais poster sur un forum anglophone. Je comprend parfaitement le francais
Bonjour NinoTLS et bienvenue,
tu pourrais faire faire la boucle de cette façon,
For Each p In ActiveSheet.DrawingObjects
If p.TopLeftCell.Column = 6 Then MsgBox "picture: " & p.Name & " à la ligne : " & p.TopLeftCell.Row
Next pBonjour i20100, merci pour ton message de bienvenue et pour ta réponse.
En effet, ton code semble plus clean et j'en prends bonne leçon.
Cependant, on est confronté au même problème, on teste tous les "DrawingObjects" de la feuille en entière, et si le DrawingObjects testé se trouve dans la colonne qui nous interresse, alors il execute ce qu'on lui demande. Tout va bien mais :
Une fois encore, je voudrais que le script ne teste QUE les DrawingObjects dans la colonne qui m'interresse.
Sachant que des objets, j'en ai une multitude dans ma worksheet, à quoi bon allez soumettre les 500 objets de ma feuille s'ils se trouvent dans d'autres colonnes qui n'ont rien à voir avec l'objet de mon script.
J'espère être assez clair... Peut-être qu'effectivement c'est la seule manière de "faire le job" car avec plusieurs jours de recherche google dans de nombreux forum dédiés, je ne trouve aucune indication vers un code qui ne ferait la boucle (for each...) que sur les images se trouvant dans un range défini (en l'occurence dans notre cas la colonne 6)
En tout cas, merci de ta réponse, sujet non résolu donc petit up et je poursuis ma quête du graal !
Nino
PS : Je dirais même plus que dans ton exemple, non seulement le script teste toutes les images de ma feuille (y compris celles qui n'interressent pas mon but) mais en plus, tous les autres objets tels que les bouttons et formes
re,
tu peux réduire le champ de recherche à la collection Pictures
mais pas plus.
re,
tu peux réduire le champ de recherche à la collection Pictures
mais pas plus.
Merci i20100... C'est donc bien ce que je pensais... Merci infiniment
On peut donc en déduire le constat suivant :
C'est un grand manque : on devrait pouvoir lancer une macro sans avoir à itérer chaque image ou shape !
Donc le fameux "for each p in activesheet.pictures" devrait être développé et/ou décliné par l'éditeur que je ne citerai pas : Microsoft
par un truc du style "for each p in activesheet.range(XY).pictures", ce qui éviterait la surcharge !
Merci encore.