假使A1放的是图片名,那么双击A1 在指定区域显示图片,双击其他单元格删除图片。
把以下代码这个放在sheet 双击事件中 代码我调试过了
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim re As Range
Dim pic As Variant
Dim path As String
Dim name As String
Dim shp As Shape
If Target.Column = 1 And Target.Row = 1 Then '双击A1
name = ThisWorkbook.Sheets(1).Cells(1, 1)
path = "C:\Users\Administrator\Desktop\" & name & ".png" '这里图片路径,我是把图片放在桌面上的,自己改
Set pic = Nothing
ThisWorkbook.Sheets(1).Range("C1:F4").Select '这里C1:F4是图片显示的区域位置,大小也和区域一样大
Set pic = ActiveSheet.Pictures.Insert(path)
pic.name = name '设定图片的名称 为A1的值
pic.Placement = xlMoveAndSize '图片随着区域大小
With pic.ShapeRange
.LockAspectRatio = msoFalse
.Height = Selection.Height
.Width = Selection.Width
End With
Else
name = ThisWorkbook.Sheets(1).Cells(1, 1)
For Each shp In ThisWorkbook.Sheets(1).Shapes '如果双击的是其他单元格,则删除A1对应的图片
If InStr(shp.name, name) Then shp.Delete
Next
End If
End Sub
可以使用VBA进行处理 Worksheet_SelectionChange 或Activate事件可以处理