用visualbasic在Excel中嵌入链接图片
如何修改以下代码,将本地临时文件夹中的链接图片嵌入实际excel文件中的每个单元格用visualbasic在Excel中嵌入链接图片,excel,vba,basic,siemens-nx,Excel,Vba,Basic,Siemens Nx,如何修改以下代码,将本地临时文件夹中的链接图片嵌入实际excel文件中的每个单元格 我不完全确定您正在做什么,但是如果您想将文件夹中的图像插入Excel,可以尝试下面的代码 Sub InsertPics() Dim fPath As String, fName As String Dim r As Range, rng As Range Application.ScreenUpdating = False fPath = "C:\Users\Public\Pictures\Sample Pi
我不完全确定您正在做什么,但是如果您想将文件夹中的图像插入Excel,可以尝试下面的代码
Sub InsertPics()
Dim fPath As String, fName As String
Dim r As Range, rng As Range
Application.ScreenUpdating = False
fPath = "C:\Users\Public\Pictures\Sample Pictures\"
Set rng = Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
i = 1
For Each r In rng
fName = Dir(fPath)
Do While fName <> ""
If fName = r.Value Then
With ActiveSheet.Pictures.Insert(fPath & fName)
.ShapeRange.LockAspectRatio = msoTrue
Set px = .ShapeRange
If .ShapeRange.Width > Rows(i).Columns(2).Width Then .ShapeRange.Width = Columns(2).Width
With Cells(i, 2)
px.Top = .Top
px.Left = .Left
.RowHeight = px.Height
End With
End With
End If
fName = Dir
Loop
i = i + 1
Next r
Application.ScreenUpdating = True
End Sub
' Note: you need the file extension, such as ',jpg', or whatever you are using, so you can match on that.
Sub Insert()
Dim strFolder As String
Dim strFileName As String
Dim objPic As Picture
Dim rngCell As Range
strFolder = "C:\Users\Public\Pictures\Sample Pictures\" 'change the path accordingly
If Right(strFolder, 1) <> "\" Then
strFolder = strFolder & "\"
End If
Set rngCell = Range("E1") 'starting cell
strFileName = Dir(strFolder & "*.jpg", vbNormal) 'filter for .png files
Do While Len(strFileName) > 0
Set objPic = ActiveSheet.Pictures.Insert(strFolder & strFileName)
With objPic
.Left = rngCell.Left
.Top = rngCell.Top
.Height = rngCell.RowHeight
.Placement = xlMoveAndSize
End With
Set rngCell = rngCell.Offset(1, 0)
strFileName = Dir
Loop
End Sub
Sub InsertPics()
Dim fPath作为字符串,fName作为字符串
变暗r为范围,rng为范围
Application.ScreenUpdating=False
fPath=“C:\Users\Public\Pictures\Sample Pictures\”
设置rng=范围(“A1:A”和单元格(Rows.Count,1).End(xlUp).Row)
i=1
对于rng中的每个r
fName=Dir(fPath)
当fName“”时执行
如果fName=r.值,则
使用ActiveSheet.Pictures.Insert(fPath&fName)
.shaperage.LockAspectRatio=msoTrue
设置px=.shaperage
如果.shaperage.Width>行(i).Columns(2).Width,则.shaperage.Width=列(2).Width
带单元(i,2)
px.Top=.Top
px.Left=.Left
.RowHeight=px.Height
以
以
如果结束
fName=Dir
环
i=i+1
下一个r
Application.ScreenUpdating=True
端接头
'注意:您需要文件扩展名,例如''jpg',或任何您正在使用的文件扩展名,以便您可以匹配该文件扩展名。
子插入()
作为字符串的Dim strFolder
将strFileName设置为字符串
暗淡的对象如图片
Dim rngCell As范围
strFolder=“C:\Users\Public\Pictures\Sample Pictures\”,相应地更改路径
如果正确(strFolder,1)“\”则
strFolder=strFolder&“\”
如果结束
设置rngCell=范围(“E1”)起始单元格
strFileName=Dir(strFolder&“*.jpg”,vbNormal)”过滤器用于.png文件
当Len(strFileName)>0时执行
Set objPic=ActiveSheet.Pictures.Insert(strFolder&strFileName)
用objPic
.Left=rngCell.Left
.Top=rngCell.Top
.Height=rngCell.RowHeight
.Placement=xlMoveAndSize
以
设置rngCell=rngCell.Offset(1,0)
strFileName=Dir
环
端接头
Sub InsertPics()
Dim fPath As String, fName As String
Dim r As Range, rng As Range
Application.ScreenUpdating = False
fPath = "C:\Users\Public\Pictures\Sample Pictures\"
Set rng = Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
i = 1
For Each r In rng
fName = Dir(fPath)
Do While fName <> ""
If fName = r.Value Then
With ActiveSheet.Pictures.Insert(fPath & fName)
.ShapeRange.LockAspectRatio = msoTrue
Set px = .ShapeRange
If .ShapeRange.Width > Rows(i).Columns(2).Width Then .ShapeRange.Width = Columns(2).Width
With Cells(i, 2)
px.Top = .Top
px.Left = .Left
.RowHeight = px.Height
End With
End With
End If
fName = Dir
Loop
i = i + 1
Next r
Application.ScreenUpdating = True
End Sub
' Note: you need the file extension, such as ',jpg', or whatever you are using, so you can match on that.
Sub Insert()
Dim strFolder As String
Dim strFileName As String
Dim objPic As Picture
Dim rngCell As Range
strFolder = "C:\Users\Public\Pictures\Sample Pictures\" 'change the path accordingly
If Right(strFolder, 1) <> "\" Then
strFolder = strFolder & "\"
End If
Set rngCell = Range("E1") 'starting cell
strFileName = Dir(strFolder & "*.jpg", vbNormal) 'filter for .png files
Do While Len(strFileName) > 0
Set objPic = ActiveSheet.Pictures.Insert(strFolder & strFileName)
With objPic
.Left = rngCell.Left
.Top = rngCell.Top
.Height = rngCell.RowHeight
.Placement = xlMoveAndSize
End With
Set rngCell = rngCell.Offset(1, 0)
strFileName = Dir
Loop
End Sub