
1、打开Excel,准备好表格。
2、在工作表中,点击菜单栏【开发工具】,在其工具栏内,点击【visual basic】。
3、接着点击菜单栏【插入】,在其下拉菜单中,选择【模块】。
4、在d出模块编辑窗口填入代码:Sub 导入图片()Dim sp As Shape, rng As Range, rg As Range, arg As RangeFor Each sp In Sheets("插入图片").ShapesIf sp.Type <>8 Thensp.Delete
Next spSet arg = Columns("a").End(xlDown)For Each rng In Range([a2], arg)pah = thisworkbook.Path &"\tupian\" &rng.Value &".jpg"。
5、在工作表的【开发工具】菜单下,插入一【表单】按钮,并与指定编写的插入图片代码关联,且进入命名为【导入图片】。
6、点击【导入图片】按钮,图片被瞬间导入。就完成了。
您好,根据您的需求,批量插入图片的参考代码如下:
Option ExplicitPublic Sub 批量插入图片()
Dim rngPicture As Range
Dim lngRow As Long
Dim intCol As Integer
With ActiveSheet
For lngRow = 1 To 16 Step 8
For intCol = 1 To 12 Step 3
Set rngPicture = .Cells(lngRow + 1, intCol + 2)
rngPicture.Select
With .Pictures.Insert("E:\坑图\" & rngPicture.Value & ".jpg").ShapeRange
.Height = 70.5
.Width = 105
End With
Next
Next
End With
Set rngPicture = Nothing
End Sub
PS:具体使用时请自行修改演示代码中的循环起止行、起止列和步进值。如果图片插入到单元格后出现偏移,请设置 Picture 的 Left 属性和 Top 属性。
另外,也可以设置图片在单元格内水平居中(比单元格大的图片暂不考虑),参考代码如下:
Option ExplicitPublic Sub 批量插入图片水平居中()
Dim rngPicture As Range
Dim lngRow As Long
Dim intCol As Integer
With ActiveSheet
For lngRow = 1 To 16 Step 8
For intCol = 1 To 12 Step 3
Set rngPicture = .Cells(lngRow + 1, intCol + 2)
rngPicture.Select
With .Pictures.Insert("E:\坑图\" & rngPicture.Value & ".jpg").ShapeRange
.Left = rngPicture.Left + (rngPicture.Width - .Width) / 2
.Top = rngPicture.Top + (rngPicture.Height - .Height) / 2
End With
Next
Next
End With
Set rngPicture = Nothing
End Sub
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)