|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
单机【J4】单元格,在右下角出现的下拉菜单中选择图片名称,此时,左侧的图片更换为指定图片名的图片。此功能的VBA代码如下:
- '用VBA代码来进行图片更换而无需插入这些图片 (vba添加图片)
- Private Sub Worksheet_Change(ByVal Target As Range)
-
- If Target.Address = "$J$4" Then
-
- Dim Pic As Object, PicPathAndName As String, PicFolder As String
-
- Dim PicT As Integer, PicL As Integer, PicH As Integer, PicW As Integer
-
- PicFolder = "图片文件夹" '图片文件夹名称
-
- PicPathAndName = ThisWorkbook.Path & "" & PicFolder & "" & Range("J4") & ".png" '所选图片路径
-
- Set Pic = ActiveSheet.Shapes("图片 1")
-
- With Pic '原图片的位置和大小
-
- PicT = .Top
-
- PicL = .Left
-
- PicH = .Height
-
- PicW = .Width
-
- End With
-
- Pic.Delete '删除原图片
-
- Set Pic = ActiveSheet.Shapes.AddPicture(Filename:=PicPathAndName, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=PicL, Top:=PicT, Width:=PicW, Height:=PicH) '插入所选图片
-
- Pic.Name = "图片 1" '设置图片名称
-
- End If
-
- Set Pic = Nothing
-
- End Sub
复制代码
附件:
【测试成功】用VBA代码来进行图片更换而无需插入这些图片 (vba添加图片).rar
(418.37 KB, 下载次数: 42)
顺便给我点点赞,留点文字呗
|
|