一次从文件夹中将多个图像插入Rows + Filename

时间:2017-01-31 17:33:21

标签: excel image vba excel-vba import

大家早上好!

所以我从extendoffice.com/documents/excel/1156-excel-insert-multiple-pictures.html获取了这段代码,因为它应该在下面看到。

我想知道的是,是否有人可以帮助我 -

将所有照片导入'行B'而不是以列方式导入。 以及如何添加'文件名' (即excel_image2.jpg)所述图像,在A'行A'中的每个图像上方。

提前感谢所有帮助!

Sub InsertPictures()
'Update 20140513
Dim PicList() As Variant
Dim PicFormat As String
Dim Rng As Range
Dim sShape As Shape
On Error Resume Next
PicList = Application.GetOpenFilename(PicFormat, MultiSelect:=True)
xColIndex = Application.ActiveCell.Column
If IsArray(PicList) Then
    xRowIndex = Application.ActiveCell.Row
    For lLoop = LBound(PicList) To UBound(PicList)
        Set Rng = Cells(xRowIndex, xColIndex)
        Set sShape = ActiveSheet.Shapes.AddPicture(PicList(lLoop), msoFalse, msoCTrue, Rng.Left, Rng.Top, Rng.Width, Rng.Height)
    xRowIndex = xRowIndex + 1
    Next
End If
End Sub

3 个答案:

答案 0 :(得分:0)

假设“B行”是“第2行”而“行A”是“第1行”,您可以试试这个:

Option Explicit

Sub InsertPictures()
    'Update 20140513
    Dim PicList() As Variant
    Dim lLoop As Long

    PicList = Application.GetOpenFilename(MultiSelect:=True)
    If IsArray(PicList) Then
        For lLoop = LBound(PicList) To UBound(PicList)
            With Cells(2, 1).Offset(, lLoop - 1)
                ActiveSheet.Shapes.AddPicture PicList(lLoop), msoFalse, msoCTrue, .Left, .top, .Width, .Height
                .Offset(-1).Value = Right(PicList(lLoop), Len(PicList(lLoop)) - InStrRev(PicList(lLoop), "\"))
            End With
        Next
    End If
End Sub

答案 1 :(得分:0)

如果您只想要文件名,请尝试以下方法:

Sub InsertPictures()
    'Update 20140513
    Dim PicList() As Variant
    Dim PicFormat As String
    Dim Rng As Range
    Dim sShape As Shape
    Dim Filename As String
    On Error Resume Next
    PicList = Application.GetOpenFilename(PicFormat, MultiSelect:=True)
    xColIndex = Application.ActiveCell.Column
    If IsArray(PicList) Then
        xRowIndex = Application.ActiveCell.Row
        For lLoop = LBound(PicList) To UBound(PicList)
            Filename = Dir(PicList(lLoop), vbDirectory)  `~~> Getting only filename from path
            Cells(xRowIndex, xColIndex) = Filename
            Set Rng = Cells(xRowIndex, xColIndex + 1)
            Set sShape = ActiveSheet.Shapes.AddPicture(PicList(lLoop), msoFalse, msoCTrue, Rng.Left, Rng.Top, Rng.Width, Rng.Height)
        xRowIndex = xRowIndex + 1
        Next
    End If
End Sub

答案 2 :(得分:0)

ManishChristian - 这只是文件名,但它位于A列和B列。

user3598756 - 让图片的'文件地址'和'第2行'进入'第1行',这正是我想要的。

我只需要 Manish的从A列和B列转到第1行和第2行,或者 user3598756 ,只需要带有扩展名的文件名,而不是完整路径。

我尝试添加“Filename = Dir(PicList(lLoop),vbDirectory)`~~>只从路径获取文件名”(没有你的评论)到Manish的lLoop但是它出错了。

由于