如何使用VBA在指定的单元格位置将图片插入到Excel中 [英] How to insert a picture into Excel at a specified cell position with VBA
本文介绍了如何使用VBA在指定的单元格位置将图片插入到Excel中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
'将图片添加到excel中
xlApp.Cells(i,20)。选择
xlApp.ActiveSheet.Pictures.Insert(picPath)。选择
'计算新图片大小
使用xlApp.Selection.ShapeRange
.LockAspectRatio = msoTrue
.Width = 75
.Height = 100
结束
'调整大小并使其可打印
使用xlApp.Selection
.Placement = 1'xlMoveAndSize
'.Placement = 2'xlMove
'.Placement = 3'xlFreeFloating
.PrintObject = True
End with
我不知道我做错了什么,但没有插入到正确的单元格中,所以我该怎么办这张图片在Excel中指定的单元格?
解决方案
尝试这样:
使用xlApp.ActiveSheet.Pictures.Insert(PicPath)
带.ShapeRange
.LockAspectRatio = msoTrue
.Width = 75
.Height = 100
结束
.Left = xlApp.ActiveSheet.Cells(i,20).Left
.Top = xlApp.ActiveSheet.Cells(我,20).Top
.Placement = 1
.PrintObject = True
结束
最好不要在Excel中选择任何东西,通常不需要,并且减慢代码的速度。
I'm adding ".jpg" files to my Excel sheet with the code below :
'Add picture to excel
xlApp.Cells(i, 20).Select
xlApp.ActiveSheet.Pictures.Insert(picPath).Select
'Calgulate new picture size
With xlApp.Selection.ShapeRange
.LockAspectRatio = msoTrue
.Width = 75
.Height = 100
End With
'Resize and make printable
With xlApp.Selection
.Placement = 1 'xlMoveAndSize
'.Placement = 2 'xlMove
'.Placement = 3 'xlFreeFloating
.PrintObject = True
End With
I don't know what I am doing wrong but it doesn't get inserted into the right cell, so what should I do to put this picture into a specified cell in Excel?
解决方案
Try this:
With xlApp.ActiveSheet.Pictures.Insert(PicPath)
With .ShapeRange
.LockAspectRatio = msoTrue
.Width = 75
.Height = 100
End With
.Left = xlApp.ActiveSheet.Cells(i, 20).Left
.Top = xlApp.ActiveSheet.Cells(i, 20).Top
.Placement = 1
.PrintObject = True
End With
It's better not to .select anything in Excel, it is usually never necessary and slows down your code.
这篇关于如何使用VBA在指定的单元格位置将图片插入到Excel中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文