select活动单元格中的图片

如何select活动单元格内的图片? 我正在尝试制作一个插入图片并将其大小调整为单元格大小的macros。 有了一些帮助,我做了下面的代码插入图片:

ActiveCell.Select Dim picname As String picname = ActiveCell.Value ActiveCell.Offset(-1, 0).Select ActiveSheet.Pictures.Insert "C:\Users\Briet\Documents\PAJ\pic-presentation\Images\" & picname & ".jpg" 

一旦图片被插入,其容器单元被选中,但不是实际的图片。 所以下面的代码将图片调整到单元格不起作用:

 On Error GoTo NOT_SHAPE Dim PicWtoHRatio As Single Dim CellWtoHRatio As Single With Selection PicWtoHRatio = .Width / .Height End With With Selection.TopLeftCell CellWtoHRatio = .Width / .RowHeight End With Select Case PicWtoHRatio / CellWtoHRatio Case Is > 1 With Selection .Width = .TopLeftCell.Width .Height = .Width / PicWtoHRatio End With Case Else With Selection .Height = .TopLeftCell.RowHeight .Width = .Height * PicWtoHRatio End With End Select With Selection .Top = .TopLeftCell.Top .Left = .TopLeftCell.Left End With Exit Sub NOT_SHAPE: MsgBox "Select a picture before running this macro." 

Insert()返回插入图片的引用,所以你可以直接使用它。

 Sub Tester() Dim shp, rng As Range Set rng = ActiveSheet.Range("C3") Set shp = ActiveSheet.Pictures.Insert("C:\_Stuff\pic.jpg") With shp .Top = rng.Top .Left = rng.Left .ShapeRange.LockAspectRatio = msoTrue .Width = .Width / Application.Max(.Width / rng.Width, _ .Height / rng.Height) End With End Sub 

如果select了单元格,则无法调整图片。 你需要select图片本身。 根据这个关于SO的问题 ,Pictures集合是没有文档的。 当我GOOGLE了,我也找不到它。 我相信,一旦你有select的单元格,你可以从ShapeRange集合中获取图片对象。 它有一个必要的索引参数,但是如果你只有一个形状,那么这个值应该是1.希望这个指向正确的方向。