|
本帖最后由 爱疯 于 2020-1-13 11:42 编辑
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
图片路径 = ThisWorkbook.Path & "\" & "照片\"
Image1.Picture = LoadPicture(图片路径 & Range("J3") & ".jpg")
Label1.Caption = Range("J3").Value
End Sub
我想实现当图片路径内没有与J3同名的图片,图片直接返回”ad.jpg"的图片。
改用这段代码吧: - Private Sub Worksheet_Change(ByVal T As Range)
- If T.Address = "$J$3" Then
- On Error Resume Next
- ActiveSheet.Unprotect (Password = "")
- mypath = ThisWorkbook.Path & "" & "照片"
- myfile = Dir(mypath & Range("J3") & ".jpg") '获取文件
- If myfile = "" Then '若文件不存在,myfile的值就是空的
- myfile = Dir(mypath & "ad.jpg")
- End If
- Set Rng = Range("J4:K8")
- For Each shp In ActiveSheet.Shapes
- If shp.Left = Rng.Left And shp.Top = Rng.Top Then shp.Delete
- Next
- ActiveSheet.Shapes.AddShape(msoShapeRectangle, Rng.Left, Rng.Top, Rng.Width, Rng.Height).Select
- Selection.ShapeRange.Fill.UserPicture mypath & myfile
- End If
- ActiveSheet.Protect (Password = "")
- Range("C4").Select
- End Sub
复制代码
|
|