Handling pictures with VBA in a worksheet

S

sec12205

How do I code the pasting of pictures in a worksheet to ensure they
don't overlap? Is there a way to find out the edge of a picture then
paste after it (or something like that)?

Thanks!
 
D

Dave Peterson

I don't see anything wrong with using the cells as my grid for my pictures:

Option Explicit
Sub testme01()

Dim myPict As Picture
Dim myAddresses As Variant
Dim iCtr As Long
Dim wks As Worksheet
Dim myPictures As Variant

Set wks = ActiveSheet

myAddresses = Array("a1:c3", "d1:f3", "g1:i3")
myPictures = Array("C:\mypict.jpg", "C:\mypict.jpg", "c:\mypict.jpg")

If UBound(myAddresses) <> UBound(myPictures) Then
MsgBox "design error"
Exit Sub
End If

With wks
For iCtr = LBound(myAddresses) To UBound(myAddresses)
With wks.Range(myAddresses(iCtr))
Set myPict = .Parent.Pictures.Insert(Filename:=myPictures(iCtr))
myPict.Top = .Top
myPict.Width = .Width
myPict.Height = .Height
myPict.Left = .Left
myPict.Placement = xlMoveAndSize
myPict.Name = "Pict_" & .Address(0, 0)
'you can even see where the picture is located
MsgBox myPict.TopLeftCell.Address(0, 0) _
& vbLf & myPict.BottomRightCell.Address(0, 0)
End With
Next iCtr
End With

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top