如何使用VBA在指定的单元格位置将图片插入Excel [英] How to insert a picture into Excel at a specified cell position with VBA
本文介绍了如何使用VBA在指定的单元格位置将图片插入Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下代码将.jpg"文件添加到我的 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
我不知道我做错了什么,但它没有插入到正确的单元格中,那么我该怎么做才能将这张图片放入 Excel 中的指定单元格?
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?
推荐答案
试试这个:
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
最好不要在 Excel 中选择任何内容,这通常是不必要的,而且会减慢您的代码速度.
It's better not to .select anything in Excel, it is usually never necessary and slows down your code.
这篇关于如何使用VBA在指定的单元格位置将图片插入Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文