Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 3844|回复: 13

[已解决]求高手写个代码

[复制链接]
发表于 2016-12-15 11:02 | 显示全部楼层 |阅读模式
求助高手解决,我想在这个表里在任一单元格上写上字,然后在这个单元格上自动更新这个文字的图片,图片大小按单元格的大小高小0.1CM长按原图比例调整,如图(即实现我输入爸在这个单元格上自动更新爸的图片,输入爸的笔顺在这个单元格上自动更新出爸的笔顺图: QQ图片20161215113202.png
最佳答案
2016-12-23 22:17
  1. Private Sub Worksheet_Change(ByVal T As Range)
  2.     If T.Count > 1 Or T.Column > 1 Then Exit Sub '当单元格不满足时退出
  3.     Dim mypath$, i&, pic As Shape, picH, shp As Shape
  4.     On Error Resume Next
  5.     For Each pic In Sheet1.Shapes '删除原来单元格图片
  6.         If pic.Name = T.Address Or pic.Name = T.Offset(0, 4).Address & "bs" Then pic.Delete
  7.     Next
  8.     mapath = ThisWorkbook.Path & "\文字库" '图片所在文件夹路径
  9.     If T.Value <> "" And Dir(mapath & T.Value & ".jpg") <> "" Then  '插入jpg文字图片
  10.         Sheet1.Shapes.AddPicture(mapath & T.Value & ".jpg", True, True, T.Left + 1, T.Top + 1, T.Width - 2, T.Height * 2 - 2).Select
  11.         Selection.Name = T.Address
  12.         With T.Offset(0, 4) '插入笔顺的单元格插入笔顺图片
  13.             If Dir(mapath & T.Value & "的笔顺.png") <> "" Then
  14.                 Sheet1.Shapes.AddPicture mapath & T.Value & "的笔顺.png", True, True, .Left, .Top, -1, -1
  15.             ElseIf Dir(mapath & T.Value & "的笔顺.jpg") <> "" Then
  16.                 Sheet1.Shapes.AddPicture mapath & T.Value & "的笔顺.jpg", True, True, .Left, .Top, -1, -1
  17.             End If
  18.             Set shp = ActiveSheet.Shapes(ActiveSheet.Shapes.Count) '设定图片大小(宽度总大于高的,故用高度确定图片高度)
  19.             shp.Left = .Left + 2 '尺寸不对可以改下下面几个数字参数
  20.             shp.Top = .Top + 2
  21.             picH = .Height / shp.Height * 0.9
  22.             shp.ScaleWidth picH, msoFalse, msoScaleFromTopLeft
  23.             shp.Name = .Address & "bs"
  24.         End With
  25.     End If
  26.     T.Offset(1).Select
  27. End Sub
复制代码

字.zip

471.43 KB, 下载次数: 6

 楼主| 发表于 2016-12-15 12:34 | 显示全部楼层
回复

使用道具 举报

发表于 2016-12-15 12:38 | 显示全部楼层
  1. Sub 插入图片()
  2.     Dim rg As Range, mypath$, pic As Shape, i&
  3.     Dim endRow&, sh As Worksheet
  4.     On Error Resume Next
  5.     endRow = [a65536].End(3).Row
  6.     Set sh = Worksheets("sheet1")
  7.     Call 删除图片
  8.     mapath = ThisWorkbook.Path & "\文字库"
  9.     For i = 1 To endRow Step 2
  10.         Set rg = Range("a" & i)
  11.         sh.Shapes.AddPicture mapath & rg.Value & ".jpg", True, True, rg.Left + 1, rg.Top + 1, rg.Width - 2, rg.Height * 2 - 2
  12.         With rg.Offset(0, 4)
  13.             If Dir(mapath & rg.Value & "的笔顺.png") <> "" Then
  14.                 sh.Shapes.AddPicture mapath & rg.Value & "的笔顺.png", True, True, .Left, .Top, .Width, .Height
  15.             ElseIf Dir(mapath & rg.Value & "的笔顺.jpg") <> "" Then
  16.                 sh.Shapes.AddPicture mapath & rg.Value & "的笔顺.jpg", True, True, .Left, .Top, .Width, .Height
  17.                
  18.             End If
  19.         End With
  20.     Next
  21. End Sub
  22. Sub 删除图片()
  23.     For Each pic In ActiveSheet.Shapes
  24.         If pic.Type = 11 Then pic.Delete
  25.     Next
  26. End Sub
复制代码

识字插入图片.zip

59.47 KB, 下载次数: 9

回复

使用道具 举报

 楼主| 发表于 2016-12-15 12:45 | 显示全部楼层

可以不要有按钮,输入字,自动插入,删除字就自动删除图片,不用点按钮更新吗??
回复

使用道具 举报

发表于 2016-12-15 13:26 | 显示全部楼层
A微微笑 发表于 2016-12-15 12:45
可以不要有按钮,输入字,自动插入,删除字就自动删除图片,不用点按钮更新吗??

已改,看下效果
  1. Private Sub Worksheet_Change(ByVal T As Range)
  2. If T.Count > 1 Then Exit Sub
  3.   Dim mypath$, i&, pic As Shape
  4.     On Error Resume Next
  5.   
  6.     mapath = ThisWorkbook.Path & "\文字库"
  7.    If T.Value <> "" And Dir(mapath & T.Value & ".jpg") <> "" Then
  8.         Sheet1.Shapes.AddPicture(mapath & T.Value & ".jpg", True, True, T.Left + 1, T.Top + 1, T.Width - 2, T.Height * 2 - 2).Select
  9.         Selection.Name = T.Address
  10.         With T.Offset(0, 4)
  11.             If Dir(mapath & T.Value & "的笔顺.png") <> "" Then
  12.                 Sheet1.Shapes.AddPicture(mapath & T.Value & "的笔顺.png", True, True, .Left, .Top, .Width, .Height).Select
  13.                 Selection.Name = T.Address & "bs"
  14.             ElseIf Dir(mapath & T.Value & "的笔顺.jpg") <> "" Then
  15.                 Sheet1.Shapes.AddPicture(mapath & T.Value & "的笔顺.jpg", True, True, .Left, .Top, .Width, .Height).Select
  16.                Selection.Name = T.Address & "bs"
  17.             End If
  18.         End With
  19.    ElseIf T.Value = "" Then
  20.         For Each pic In Sheet1.Shapes
  21.             If pic.Name = T.Address Or pic.Name = T.Address & "bs" Then pic.Delete
  22.         Next
  23.   
  24.   End If
  25.   T.Offset(1).Select
  26. End Sub
复制代码


字改.zip

367.12 KB, 下载次数: 11

回复

使用道具 举报

 楼主| 发表于 2016-12-15 13:38 | 显示全部楼层

下载了,用不了的
回复

使用道具 举报

发表于 2016-12-15 14:08 | 显示全部楼层
A微微笑 发表于 2016-12-15 13:38
下载了,用不了的

宏要启用的,文件-选项-信任中心-信任中心设置-宏设置-启用所有宏(或者禁用所有宏,并发出通知)
回复

使用道具 举报

 楼主| 发表于 2016-12-23 13:48 | 显示全部楼层
苏子龙 发表于 2016-12-15 14:08
宏要启用的,文件-选项-信任中心-信任中心设置-宏设置-启用所有宏(或者禁用所有宏,并发出通知)

亲,不知道为什么?还是不行哦,能帮再改改吗??
回复

使用道具 举报

 楼主| 发表于 2016-12-23 13:53 | 显示全部楼层
苏子龙 发表于 2016-12-15 14:08
宏要启用的,文件-选项-信任中心-信任中心设置-宏设置-启用所有宏(或者禁用所有宏,并发出通知)

大神,还是不行啊,能帮再改一下吗??
回复

使用道具 举报

发表于 2016-12-23 15:46 | 显示全部楼层
请看动画,图片文件夹,要和excel文件放在同一路径下
爸妈地加入图片.gif
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-26 11:42 , Processed in 0.385532 second(s), 12 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表