Excel批量导入图片的方法实例教程
时间:2023-12-04 11:46:51
excel批量导入图片一般是使用VBA代码实现,因为excel功能的局限性,插入——图片,尽管可以批量导入图片,但不能自动排版。
文章总结几个excel批量导入图片相关的案例,分享给大家。
excel批量导入图片案例一:
如下图所示:A列是姓名,D列需要批量导入A列姓名对应的照片,当单击“图片导入”按钮,即可批量导入图片。
下面的代码实现的功能是:批量导入图片,然后按照原比例缩放以填充单元格,如果单元格高宽改变,再次单击“图片导入”按钮即可。
说明:对应的图片命名需要和姓名一致,图片文件夹和excel源文件放在同一路径之下。
下面是具体的代码:
Sub 批量导入图片()
‘将图片导入。
‘图片按照原比例存储,按照原比例存储
On Error Resume Next
Dim R&
Dim Pic As Object
‘先删除所有可能存在的图片
For Each Pic In Sheet1.Shapes
If Pic.Name <> Sheet1.Shapes("按钮 97").Name Then
Pic.Delete
End If
Next
For R = 2 To Range("A65536").End(xlUp).Row
‘插入图片
Set Pic = Sheet1.Pictures.Insert(ThisWorkbook.Path & "\pic\" & Cells(R, 1) & ".jpg")
‘锁定高宽比
Pic.ShapeRange.LockAspectRatio = True
‘看高宽比。如果图片高宽比高,那么调整到单元格高度,否则调整到单元格宽度
‘我们看到的右键格式菜单里的东西都是针对ShapeRange而言的,所以要用ShapeRange来设定
With Pic.ShapeRange
‘如果图片高宽比比单元格大,说明图片太高,只需调整图片高度
If .Height / .Width > Cells(R, 4).Height / Cells(R, 4).Width Then
.Height = Cells(R, 4).Height
‘调整位置
.Top = Cells(R, 4).Top
.Left = Cells(R, 4).Left + (Cells(R, 4).Width – .Width) / 2
‘如果图片高宽比比单元格小,说明图片太宽,只需调整图片宽度
Else
.Width = Cells(R, 4).Width
‘调整位置
.Left = Cells(R, 4).Left
.Top = Cells(R, 4).Top + (Cells(R, 4).Height – .Height) / 2
End If
End With
Next R
End Sub
我们在excel对图片进行排版时,可以选中图片,勾选对齐网格,以及在插入图片或控件时,按住Alt键并拖动对象的边框,可以快速调整尺寸适应单元格大小。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
excel2007表格中如何插入和编辑剪贴画
![](https://img.aspxhome.com/file/2023/9/a140389_0s.jpg)
Word2016如何使用自带的截屏功能
![](https://img.aspxhome.com/file/2023/4/17734_0s.jpg)
WPS 如何在Excel中给不足位数的数字前面补0
![](https://img.aspxhome.com/file/2023/1/a185821_0s.jpg)
wps怎么制作和美化表格 wps表格基本操作教程
![](https://img.aspxhome.com/file/2023/9/a187409_0s.jpg)
winhelp.exe - winhelp是什么进程
怎么删除Win10系统使用痕迹 删除Win10系统使用痕迹的方法
![](https://img.aspxhome.com/file/2023/25/a218315_0s.jpg)
Windows XP系统关闭磁盘索引的两个方法图文教程
![](https://img.aspxhome.com/file/2023/29/a253919_0s.jpg)
Word2010复制网页上的内容自动清除超链接、图片等格式样式
腾讯会议如何开启全体成员可录制权限
![](https://img.aspxhome.com/file/2023/9/a341121_0s.png)
iMac Cleaner for Mac如何卸载应用程序?iMac Cleaner Ma的使用方法
![](https://img.aspxhome.com/file/2023/2/a214082_0s.png)
PPT图表美化教程二:单系列柱状图的美化
笔记本电脑重装系统提示Decompression error Abort怎么办?
![](https://img.aspxhome.com/file/2023/6/a325085_0s.jpg)
Excel中进行设置表格底色的操作技巧
Mac上重复文件太多,怎么清理省时省力呢?
![](https://img.aspxhome.com/file/2023/0/a209230_0s.jpeg)
使用4k video downloader mac如何从YouTube下载2160p超清HD视频
![](https://img.aspxhome.com/file/2023/5/a213315_0s.png)
微软KB3184143补丁彻底移除GWX win10升级程序 附补丁更新包下载
![](https://img.aspxhome.com/file/2023/29/a260910_0s.jpg)
Win11分辨率改不了怎么办?
![](https://img.aspxhome.com/file/2023/1/a282443_0s.jpg)
如何更改QQ默认浏览器?更改QQ默认浏览器的方法
![](https://img.aspxhome.com/file/2023/9/a340849_0s.jpg)
为什么鼠标单击变双击?鼠标单击变双击如何解决?
![](https://img.aspxhome.com/file/2023/6/a326604_0s.jpg)
[下载]微软Office 2016预览版发布 内附下载地址
![](https://img.aspxhome.com/file/2023/6/15586_0s.jpg)