| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 567 人关注过本帖
标题:求助,VBA插入图片
只看楼主 加入收藏
x279517904
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2023-4-26
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
求助,VBA插入图片
单元格AJ35中,输入图片文件名,在g48:w62插入对应BMP图片,现在想在a233:q237也插入此图片,应该怎样修改以下语句?

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, fp$, shnm$, nm$, shp As Shape
Dim tp, lf, wd
Set rng = [g48:w62]
If Target.Count > 1 Then Exit Sub
If Target = "" Then Exit Sub
If Target.Address <> "$AJ$35" Then Exit Sub
fp = ThisWorkbook.Path & "\图片\"
nm = fp & "\" & Target.Value & ".bmp"
For Each shp In ActiveSheet.Shapes
If shp.Type = msoAutoShape Then
shp.Delete
End If
Next
nn = Dir(nm)
If nn <> "" Then
With rng
ML = .Left
MT = .Top
MW = .Width
MH = .Height
ActiveSheet.Shapes.AddShape(msoShapeRectangle, ML, MT, MW, MH).Select
Selection.ShapeRange.Fill.UserPicture nm
End With
End If
Target.Select
End Sub
搜索更多相关主题的帖子: If Target 图片 Then Sub 
2023-04-26 20:41
东海ECS
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:Python
等 级:版主
威 望:32
帖 子:412
专家分:1646
注 册:2023-1-24
收藏
得分:10 
可以通过将原代码中的 rng 变量修改为 a233:q237 的范围来实现在 a233:q237 中插入相应的图片。修改后的代码如下:

Private Sub Worksheet_Change(ByVal Target As Range) Dim rng As Range, fp$, shnm$, nm$, shp As Shape Dim tp, lf, wd '将 rng 变量修改为 a233:q237 的范围 Set rng = [a233:q237] If Target.Count > 1 Then Exit Sub If Target = "" Then Exit Sub If Target.Address <> "$AJ$35" Then Exit Sub fp = ThisWorkbook.Path & "\图片" nm = fp & "" & Target.Value & ".bmp" For Each shp In ActiveSheet.Shapes If shp.Type = msoAutoShape Then shp.Delete End If Next nn = Dir(nm) If nn <> "" Then With rng ML = .Left MT = .Top MW = .Width MH = .Height ActiveSheet.Shapes.AddShape(msoShapeRectangle, ML, MT, MW, MH).Select Selection.ShapeRange.Fill.UserPicture nm End With End If Target.Select End Sub


注意,修改后代码中的 rng 变量仅影响图片位置和大小,图片文件名仍是从单元格 AJ35 中获取,所以需要确保在单元格 AJ35 中输入的文件名与图片文件名一致。如果需要从其他单元格中获取文件名,可以对代码进行相应的修改。




会当凌绝顶,一览众山小.
2023-04-27 19:22
阳光上的桥
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:38
帖 子:129
专家分:772
注 册:2023-1-12
收藏
得分:10 
修改后的程序代码如下,说明见注释:
程序代码:
Option Explicit '这个是好东西,建议都整起

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range, fp$, shnm$, nm$, shp As Shape
    Dim tp, lf, wd
    If Target.Count > 1 Then Exit Sub
    If Target = "" Then Exit Sub
    Select Case Target.Address
        Case "$AJ$35": Set rng = [g48:w62]  'AJ35文件名插入的地方
        Case "$AJ$36": Set rng = [a233:q237] 'AJ36文件名插入的地方,可以依葫芦画瓢自行添加更多的行
        Case Else: Exit Sub
    End Select
    fp = ThisWorkbook.Path & "\图片\"
    nm = fp & "\" & Target.Value & ".bmp"
    For Each shp In ActiveSheet.Shapes
        If shp.Type = msoAutoShape Then
            If shp.Left = rng.Left And shp.Top = rng.Top Then shp.Delete '这里很关键,只删除rng那个图,不能全删
        End If
    Next
    If Dir(nm) <> "" Then
        With rng
            ActiveSheet.Shapes.AddShape(msoShapeRectangle, .Left, .Top, .Width, .Height).Select
            Selection.ShapeRange.Fill.UserPicture nm
        End With
    End If
    Target.Select
End Sub
2023-04-28 08:49
快速回复:求助,VBA插入图片
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013693 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved