5,139
社区成员
发帖
与我相关
我的任务
分享
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Len(Target.Text) = 0 Then Exit Sub
Dim sngLeft As Single, sngTop As Single, sngRight As Single, sngBottom As Single, sngScale As Single
Dim rngCell As Range, rngCellBR As Range, shp As Shape, tmp
If Dir(Target.Text) <> "" Then
Set rngCell = Target.Offset(0, 1)
Set rngCellBR = rngCell.Offset(1, 1)
sngTop = rngCell.Top
sngLeft = rngCell.Left
sngRight = rngCellBR.Left
sngBottom = rngCellBR.Top
For Each shp In ActiveSheet.Shapes
If shp.Top >= sngTop - 5 And shp.Top < sngBottom - 5 And shp.Left >= sngLeft - 5 And shp.Left < sngRight - 5 Then
shp.Delete
Exit For
End If
Next shp
rngCell.Select
On Error GoTo ErrorHandler
ActiveSheet.Pictures.Insert(Target.Text).Select
Set shp = Selection.ShapeRange(1)
shp.Top = rngCell.Top
shp.Left = rngCell.Left
sngScale = rngCell.Width / shp.Width
shp.ScaleWidth sngScale, msoFalse, msoScaleFromTopLeft
shp.ScaleHeight sngScale, msoFalse, msoScaleFromTopLeft
rngCell.Rows.RowHeight = shp.Height
shp.Placement = xlMoveAndSize
ErrorHandler:
Set shp = Nothing
Set rngCell = Nothing
Set rngCellBR = Nothing
Set Target = Nothing
End If
End Sub