且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Excel - 删除行时删除图像

更新时间:2023-12-05 17:20:16

您可以将图片属性更改为移动并使用单元格大小。因此,当您删除行时,您的图像也将被删除。
在Excel 2007中测试。



另一个解决方案是添加评论并在后台填充图片(请参阅更多信息: http://www.excelforum.com/excel-general/569566-embed-image -in-cell.html


I have a Macro that imports images from a directory and places them in excel cells that are made just big enough to fit the image in

A snippet of the macro is below:-

'Set the Row Height and Column Width of the thumbnail

Range("A" & CStr(currRow)).RowHeight = ThumbnailSizeRef + 2 

Columns("A").ColumnWidth = (ThumbnailSizeRef - 5) / 5 'Column Width uses a font width setting, this is the formula to convert to pixels

'Add the thumbnail
Set sShape = ActiveSheet.Shapes.AddPicture(Filename:=sFilename, LinktoFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=0, Top:=0, Width:=ThumbnailSizeRef, Height:=ThumbnailSizeRef)

'Set the Left and Top position of the Shape
sShape.Left = Range("A" & CStr(currRow)).Left + ((Range("A" & CStr(currRow)).Width - sShape.Width) / 2)

sShape.Top = Range("A" & CStr(currRow)).Top + ((Range("A" & CStr(currRow)).Height - sShape.Height) / 2)

This all works fine. The images display correctly just in the cell as required. I can sort the cells successfully also and the images get moved correctly.

The problem I have is when I Delete an entire Row (right click on the row and delete)...in this situation the image from the row Im deleting jumps down and hides behind the image on the next row.

Is there any way that when I delete the row the image gets deleted as well?

You can change your picture properties to "Move and size with cells". Hence, when you delete your row, your image will be deleted too. Tested in Excel 2007.

Another solution is to add a comment and fill the picture in the background (see more info here : http://www.excelforum.com/excel-general/569566-embed-image-in-cell.html)