excel表格如何使用宏在当前单元格内插入图片
对于宏我是一窍不通啊!!谁能帮帮我??帮我编一个代码?? 比如我想在A1单元格内,输入V10,照片文件夹里名称为V10的照片就会插入A1单元格。在B1,或者任意单元格内输入V11,V11的照片就会插入到当前单元格内。 展开
代码如下先给你,我先声明下,我也是之前需要这个操作在网上找的,可以实现你的功能,很好用。
具体用法:
打开excel的VB,把代码复制进去。
在A列输入需要插入照片的名称,然后选定这些有名称的单元格(重点!要选定)。
然后运行宏,在弹出的对话框选【否】,意思就是在名称的右边插入图片,上面的汉字应该也能看懂。
然后会出来一个对话框让你选择路径,选择你放照片的那个文件夹,点击文件夹里的任意一张照片点【打开】。
然后照片就插进去了,而且是每张照片都填满单元格。
建议提前把单元格的大小设置好然后再运行宏。
如下看我的示例,功能真鸡儿强大,再次膜拜下VB大神。
Sub A()
Dim Rng As Range
Set Rng = Selection
K = MsgBox("Yes=按姓名行下插入,No=按姓名列右挿入,Cancel=直接覆盖插入", vbYesNoCancel)
If K = vbYes Then
r = 1: c = 0
ElseIf K = vbNo Then
r = 0: c = 1
Else
r = 0: c = 0
End If
Pf = "ai,"
Pf = Pf & "bmp,bmz"
Pf = Pf & "cdr,cgm,"
Pf = Pf & "dib,dwg,dxf,"
Pf = Pf & "emf,emz,eps,exf,exif,"
Pf = Pf & "fpx,"
Pf = Pf & "gfa,gif,"
Pf = Pf & "hdr,"
Pf = Pf & "ico,"
Pf = Pf & "jfif,jpe,jpeg,jpg,"
Pf = Pf & "pcd,pct,pcx,pcz,pict,png,psd,"
Pf = Pf & "raw,rle,"
Pf = Pf & "svg,"
Pf = Pf & "tga,tif,tiff,"
Pf = Pf & "ufo,"
Picformat = Pf & "wdp,wmf,wmz,"
OpenFile = Application.GetOpenFilename("Picture Files(*.*),*.*", , "打开目的档案夹後选择任一图片即可指定资料夹。或按取消则会将当前档所在资料夹认作指定资料夹。")
If OpenFile = False Then
myDir = ThisWorkbook.Path & "\"
Else
myDir = Left(OpenFile, InStrRev(OpenFile, "\"))
End If
Application.ScreenUpdating = False
Filename = Dir(myDir)
Do While Filename <> ""
If InStr(Picformat, LCase(Right(Filename, Len(Filename) - InStrRev(Filename, ".")))) > 0 Then
PicName = Left(Filename, InStrRev(Filename, ".") - 1)
Rng.Select
On Error Resume Next
Selection.Find(What:=PicName, After:=ActiveCell, LookAt:=xlWhole).Activate
If Err.Number <> 0 Then
Err.Clear
Else
ActiveSheet.Pictures.Insert(myDir & Filename).Select
With Selection
.Placement = xlMoveAndSize
.ShapeRange.LockAspectRatio = msoFalse
.Top = ActiveCell.Offset(r, c).Top
.Left = ActiveCell.Offset(r, c).Left
.Height = ActiveCell.Offset(r, c).Height
.Width = ActiveCell.Offset(r, c).Width
End With
End If
End If
Filename = Dir
Loop
Rng.Select
End Sub
您要什么文件,我给你弄!!
就是你要操作的文件啊!