Ms office 将图像从文件夹插入excel单元格
我是VBA程序的新手,我不知道如何执行我的要求 在这里,我的要求是 在我的Excel表格中有3列列名(序列号、序列号、E)。 我想根据匹配的S.no和图像名称将图像插入到S和E列中,我的所有图像都在另一个文件夹中 样本输入格式Ms office 将图像从文件夹插入excel单元格,ms-office,Ms Office,我是VBA程序的新手,我不知道如何执行我的要求 在这里,我的要求是 在我的Excel表格中有3列列名(序列号、序列号、E)。 我想根据匹配的S.no和图像名称将图像插入到S和E列中,我的所有图像都在另一个文件夹中 样本输入格式 S.no S E 1 2 99 文件夹中的图像名称 c:\iamges\E_001.jpg c:\images\E_002.jpg c:\images\S_002.jpg c:\images\E_
S.no S E
1
2
99
文件夹中的图像名称
c:\iamges\E_001.jpg
c:\images\E_002.jpg
c:\images\S_002.jpg
c:\images\E_099.jpg
单元格中所需的输出格式
S.no S E
1 E_001.jpg
2 S_002.jpg E_002.jpg
99 E_099.jpg
这里S.no.1与E_001.jpg图像匹配
S.no 2正在匹配文件夹中的S_002.jpg和E_002.jpg图像
以类似的方式匹配所有图像并填充到单元格中
我正在尝试以下代码
strFolder = "C:\\images" 'change the path accordingly
If Right(strFolder, 1) <> "\" Then
strFolder = strFolder & "\"
End If
Set rngCell = Range("c5") 'starting cell
strFileName = Dir(strFolder & "E*.jpg", vbNormal) 'filter for .jpg files
Do While Len(strFileName) > 0
Set objPic = ActiveSheet.Pictures.Insert(strFolder & strFileName)
With objPic
.ShapeRange.LockAspectRatio = False
.Left = rngCell.Left
.Top = rngCell.Top
.Height = rngCell.Height
.Width = rngCell.Width
.Placement = xlMoveAndSize
End With
Set rngCell = rngCell.Offset(1, 0)
strFileName = Dir
Loop
strFolder=“C:\\images”相应地更改路径
如果正确(strFolder,1)“\”则
strFolder=strFolder&“\”
如果结束
设置rngCell=范围(“c5”)起始单元格
strFileName=Dir(strFolder&“E*.jpg”,vbNormal)”过滤器用于.jpg文件
当Len(strFileName)>0时执行
Set objPic=ActiveSheet.Pictures.Insert(strFolder&strFileName)
用objPic
.ShapeRange.LockAspectRatio=False
.Left=rngCell.Left
.Top=rngCell.Top
.Height=rngCell.Height
.Width=rngCell.Width
.Placement=xlMoveAndSize
以
设置rngCell=rngCell.Offset(1,0)
strFileName=Dir
环
上面的代码将所有图像填充到单元格中,但不匹配文件名和S.no。我根据引用尝试了
Sub AddPictures()
Dim myPic As Picture
Dim wkSheet As Worksheet
Dim myRng As Range
Dim myCell As Range
Dim rowCount2 As Long
Set wkSheet = Sheets(2) ' -- Working sheet
'-- The usual way of finding used row count for specific column
rowCount2 = wkSheet.Cells(wkSheet.Rows.Count, "A").End(xlUp).Row
If rowCount2 <> 0 Then
Set myRng = wkSheet.Range("A2", wkSheet.Cells(wkSheet.Rows.Count, "A").End(xlUp)) 'S.no starting from cell A2
For Each myCell In myRng.Cells
If Len(myCell) = 1 Then
myCell2 = "E_00" & myCell & ".jpg"
myCell3 = "S_00" & myCell & ".jpg"
ElseIf Len(myCell) = 2 Then
myCell2 = "E_0" & myCell & ".jpg"
myCell3 = "S_0" & myCell & ".jpg"
Else
myCell2 = "E_" & myCell & ".jpg"
myCell3 = "S_" & myCell & ".jpg"
End If
myCell1 = "c:\iamges\\\" & myCell2
If Trim(myCell1) = "" Then
MsgBox "No file path"
ElseIf Dir(CStr(myCell1)) = "" Then
MsgBox "Error Image" & myCell & " Doesn't exist!"
Else
Set myPic = myCell.Offset(0, 1).Parent.Pictures.Insert(myCell1)
With myPic '1 columns to the right of A ( is B)
'-- resize image here to fit into the size of your cell
.ShapeRange.LockAspectRatio = False
myPic.Top = myCell.Offset(0, 1).Top
myPic.Width = myCell.Offset(0, 1).Width
myPic.Height = myCell.Offset(0, 1).Height
myPic.Left = myCell.Offset(0, 1).Left
myPic.Placement = xlMoveAndSize
End With
End If
myCell1 = "c:\iamges\\\" & myCell3
If Trim(myCell1) = "" Then
MsgBox "No file path"
ElseIf Dir(CStr(myCell1)) = "" Then
MsgBox "Solution image " & myCell & " Doesn't exist!"
Else
'myCell.Offset(0, 1).Parent.Pictures.Insert (myCell1)
Set myPic = myCell.Offset(0, 2).Parent.Pictures.Insert(myCell1)
With myPic '1 columns to the right of A ( is C)
'-- resize image here to fit into the size of your cell
.ShapeRange.LockAspectRatio = False
myPic.Top = myCell.Offset(0, 2).Top
myPic.Width = myCell.Offset(0, 2).Width
myPic.Height = myCell.Offset(0, 2).Height
myPic.Left = myCell.Offset(0, 2).Left
myPic.Placement = xlMoveAndSize
End With
End If
Next myCell
Else
MsgBox "File is Empty"
End If
End Sub
子添加图片()
像图片一样模糊的myPic
将工作表设置为工作表
暗myRng As范围
暗淡的迈塞尔山脉
暗行数与长行数相同
设置工作表=工作表(2)'--工作表
'--查找特定列的已用行数的常用方法
rowCount2=wkSheet.Cells(wkSheet.Rows.Count,“A”).End(xlUp).Row
如果rowCount2为0,则
设置myRng=wkSheet.Range(“A2”,wkSheet.Cells(wkSheet.Rows.Count,“A”).End(xlUp))的S.no从单元格A2开始
对于myRng.细胞中的每个菌丝体
如果Len(myCell)=1,则
myCell2=“E_00”&myCell&“.jpg”
myCell3=“S_00”&myCell&“.jpg”
ElseIf Len(myCell)=2然后
myCell2=“E_0”&myCell&“.jpg”
myCell3=“S_0”&myCell&“.jpg”
其他的
myCell2=“E_”&myCell&“.jpg”
myCell3=“S_”&myCell&“.jpg”
如果结束
mycell=“c:\iamges\\\”&mycell 2
如果修剪(菌丝体1)=“那么
MsgBox“无文件路径”
ElseIf Dir(CStr(mycell 1))=“然后
MsgBox“错误图像”&myCell&“不存在!”
其他的
设置myPic=myCell.Offset(0,1).Parent.Pictures.Insert(myCell 1)
myPic'1列位于A的右侧(是B)
“--在此调整图像大小以适合单元格大小
.ShapeRange.LockAspectRatio=False
myPic.Top=myCell.Offset(0,1).Top
myPic.Width=myCell.Offset(0,1).Width
myPic.Height=myCell.Offset(0,1).Height
myPic.Left=myCell.Offset(0,1).Left
myPic.Placement=xlMoveAndSize
以
如果结束
mycell=“c:\iamges\\\”&mycell 3
如果修剪(菌丝体1)=“那么
MsgBox“无文件路径”
ElseIf Dir(CStr(mycell 1))=“然后
MsgBox“解决方案映像”&myCell&“不存在!”
其他的
'myCell.Offset(0,1).Parent.Pictures.Insert(myCell 1)
设置myPic=myCell.Offset(0,2).Parent.Pictures.Insert(myCell1)
myPic'1列位于A的右侧(是C)
“--在此调整图像大小以适合单元格大小
.ShapeRange.LockAspectRatio=False
myPic.Top=myCell.Offset(0,2).Top
myPic.Width=myCell.Offset(0,2).Width
myPic.Height=myCell.Offset(0,2).Height
myPic.Left=myCell.Offset(0,2).Left
myPic.Placement=xlMoveAndSize
以
如果结束
下一个迈塞尔
其他的
MsgBox“文件为空”
如果结束
端接头
参考