在以文件名为参考的excel宏中插入图片

在以文件名为参考的excel宏中插入图片,excel,vba,Excel,Vba,我有一些作业,需要将与该行的样式编号对应的图像插入excel。 我习惯于一张一张地插入图片,然后调整大小,这可能会很耗时,也很令人沮丧 我从几个不同的地方整理了一个vba脚本,它做得非常好。我从一个地方得到循环,从另一个地方得到CenterMe Sub,从第三个地方得到IsFile。你所需要做的就是把文件路径放在第四列,它会在第一列为你放一张大小完美的图片 唯一的问题是,您需要为图像指定一个高度,然后确保所有框都大于该高度。您还需要指定范围 如果有人能想出一个解决方案,自动计算出单元格的高度,并

我有一些作业,需要将与该行的样式编号对应的图像插入excel。 我习惯于一张一张地插入图片,然后调整大小,这可能会很耗时,也很令人沮丧

我从几个不同的地方整理了一个vba脚本,它做得非常好。我从一个地方得到循环,从另一个地方得到CenterMe Sub,从第三个地方得到IsFile。你所需要做的就是把文件路径放在第四列,它会在第一列为你放一张大小完美的图片

唯一的问题是,您需要为图像指定一个高度,然后确保所有框都大于该高度。您还需要指定范围

如果有人能想出一个解决方案,自动计算出单元格的高度,并通过查找excel的末尾来计算出范围,那么它将是完美的。 代码如下:

' I set RowHeight in the excel to 125 and Picture Height to 100 so it fits nicely
' into the box that I want.
' That can be changed to suit your needs.
Sub InsertImageFullName()

    Application.ScreenUpdating = False

    Dim pic As String ' File path of a picture
    Dim cl As Range
    Dim i As Integer

    Set Rng = Range("A2:A117") ' Defining input range
    i = 1
    For Each cl In Rng

        pic = cl.Offset(0, 3)  ' Full path of the picture file:
                               ' Located in the same row, third column from A, i.e. column D
        If IsFile(pic) Then

            Set myPicture = ActiveSheet.Pictures.Insert(pic) ' Inserting picture from address in D column
                                                             ' into column A

            With myPicture ' Setting picture properties
                .ShapeRange.LockAspectRatio = msoTrue ' Keep aspect ratio
                .Height = 100 ' Set your own size
                .Top = Rows(cl.Row).Top
                .Left = Columns(cl.Column).Left
                .Placement = xlMoveAndSize
            End With
            CenterMe ActiveSheet.Shapes(i), cl
            i = i + 1
        End If

    Next    ' Looping to the Nth row, defined in:
            ' " Set Rng = Range("A13:A20") "

    Set myPicture = Nothing

    Application.ScreenUpdating = True

End Sub
Sub CenterMe(Shp As Shape, OverCells As Range)

    With OverCells
        Shp.Left = .Left + ((.Width - Shp.Width) / 2)
        Shp.Top = .Top + ((.Height - Shp.Height) / 2)
    End With
End Sub
Function IsFile(ByVal fName As String) As Boolean
'Returns TRUE if the provided name points to an existing file.
'Returns FALSE if not existing, or if it's a folder
    On Error Resume Next
    IsFile = ((GetAttr(fName) And vbDirectory) <> vbDirectory)
End Function
“我在excel中将行高设置为125,将图片高度设置为100,这样它就非常适合了
“放进我想要的盒子里。
“这可以根据您的需要进行更改。
子InsertImageFullName()
Application.ScreenUpdating=False
Dim pic作为图片的字符串文件路径
Dim cl As范围
作为整数的Dim i
设置Rng=范围(“A2:A117”)'定义输入范围
i=1
对于Rng中的每个cl
pic=cl.Offset(0,3)'图片文件的完整路径:
'位于同一行,A的第三列,即D列
如果是文件(pic),则
设置myPicture=ActiveSheet.Pictures.Insert(pic)'从D列中的地址插入图片
'进入A列
使用myPicture设置图片属性
.ShapeRange.LockAspectRatio=msoTrue“保持纵横比”
.Height=100'设置您自己的尺寸
.Top=行(cl.Row)。Top
.左=列(cl.列)。左
.Placement=xlMoveAndSize
以
CenterMe活动表。形状(i),cl
i=i+1
如果结束
“下一步”循环到第n行,在中定义:
“设置Rng=范围(“A13:A20”)”
设置myPicture=Nothing
Application.ScreenUpdating=True
端接头
副中心(Shp作为形状,覆盖作为范围)
带外套
左上角=.Left+(.Width-Shp.Width)/2)
上盘高度=.Top+(.Height-Shp.Height)/2)
以
端接头
函数IsFile(ByVal fName作为字符串)作为布尔值
'如果提供的名称指向现有文件,则返回TRUE。
'如果不存在或是文件夹,则返回FALSE
出错时继续下一步
IsFile=((GetAttr(fName)和vbDirectory)vbDirectory)
端函数