Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 循环问题以填充电子表格_Excel_Vba - Fatal编程技术网

Excel 循环问题以填充电子表格

Excel 循环问题以填充电子表格,excel,vba,Excel,Vba,我有数百张照片,我想在Excel中嵌入它们的文件名。我想填充电子表格:向下10行A1-A10,然后从A1-BT10向右 到目前为止,我拥有的VBA具有以下功能: VBA请求包含照片的文件夹 设置列宽和行高以接受文件名/图像 将列宽A:BT设置为整个文件名和图像 在A:1中嵌入文件名,在B:1 重复嵌入,直到嵌入所有文件名和图像,例如,但仅在列A/B中 我需要嵌入的对象横跨电子表格,而不是仅仅向下,即向下10行–向上和向右–向下10行–向上和向右,等等 最好有一个包含几百个JPEG的文件夹,然后运

我有数百张照片,我想在Excel中嵌入它们的文件名。我想填充电子表格:向下10行
A1-A10
,然后从
A1-BT10
向右

到目前为止,我拥有的VBA具有以下功能:

  • VBA请求包含照片的文件夹
  • 设置列宽和行高以接受文件名/图像
  • 将列宽
    A:BT
    设置为整个文件名和图像
  • A:1
    中嵌入文件名,在
    B:1
  • 重复嵌入,直到嵌入所有文件名和图像,例如,但仅在列A/B中
  • 我需要嵌入的对象横跨电子表格,而不是仅仅向下,即向下10行–向上和向右–向下10行–向上和向右,等等

    最好有一个包含几百个JPEG的文件夹,然后运行代码。附件显示了预期布局

    按所需方式显示填充的工作表

    在不同点尝试循环,但失败

    Option Explicit
    
    Sub EmbedImages()
        Dim mainWorkBook As Workbook
        Dim Flder As FileDialog
        Dim Folderpath, fStr, myPath, Filename, getFolder As String
        Dim fso, NoOfFiles, listfiles, fls, strCompFilePath
        Dim counter
        ' Get source images folder
        Set Flder = Application.FileDialog(msoFileDialogFolderPicker)
        With Flder
            .Title = "Select the folder containing data"
            .AllowMultiSelect = True
            If .Show <> -1 Then GoTo NextCode
            Folderpath = .SelectedItems(1)
        End With
    NextCode:
        getFolder = Folderpath
        Set Flder = Nothing
        Set mainWorkBook = ActiveWorkbook
        Sheets("Sheet1").Activate
        Set fso = CreateObject("Scripting.FileSystemObject")
        NoOfFiles = fso.getFolder(Folderpath).Files.Count
        Set listfiles = fso.getFolder(Folderpath).Files
        For Each fls In listfiles
            strCompFilePath = Folderpath & "\" & Trim(fls.Name)
            If strCompFilePath <> "" Then
                ' // include image extensions here \\
                If (InStr(1, strCompFilePath, "jpg", vbTextCompare) > 1 _
                    Or InStr(1, strCompFilePath, "jpeg", vbTextCompare) > 1 _
                    Or InStr(1, strCompFilePath, "png", vbTextCompare) > 1) Then
                    counter = counter + 1
                    Filename = fls.Name
                    If InStr(Filename, ".") > 0 Then
                        Filename = Left(Filename, InStr(Filename, ".") - 1)
                    End If
                    ' Set alignment as AlignCenter
                    ActiveSheet.Columns("A:BT").VerticalAlignment = xlVAlignCenter
                    ' Sets WrapText
                    ActiveSheet.Range("A:BT").Select
                    With Selection
                        .WrapText = True
                    End With
                    ' Insert Filename
                    ActiveSheet.Range("A" & counter).Value = Filename
                    ' Set ColumnWidth for 29 FileNames/Images
                    ActiveSheet.Range("A:B,D:G,I:L,N:Q,S:V,X:AA,AC:AF,AH:AK,AM:AP,AR:AU,AW:AZ,BB:BE,BG:BJ,BL:BO,BQ:BT").ColumnWidth = 19
                    ' Sets RowHeight for scaled image
                    ActiveSheet.Range("B" & counter).RowHeight = 70
                    ActiveSheet.Range("B" & counter).Activate
                    Call insert(strCompFilePath, counter)
                    ActiveSheet.Activate
                End If
            End If
        Next
        'mainWorkBook.Save
    End Sub
    
    Function insert(PicPath, counter)
        'MsgBox PicPath
        With ActiveSheet.Pictures.insert(PicPath)
            '// change image sizes here \\
            With .ShapeRange
                .LockAspectRatio = msoTrue
                .Width = 50
                .Height = 70
            End With
            .Left = ActiveSheet.Range("B" & counter).Left
            .Top = ActiveSheet.Range("B" & counter).Top
            .Placement = 1
            .PrintObject = True
        End With
    End Function
    
    选项显式
    子图像()
    将工作簿设置为工作簿
    Dim Flder As FILE对话框
    Dim Folderpath、fStr、myPath、文件名、getFolder作为字符串
    Dim fso、NoOfFiles、listfiles、fls、strCompFilePath
    暗色计数器
    '获取源图像文件夹
    Set Flder=Application.FileDialog(msoFileDialogFolderPicker)
    与弗尔德
    .Title=“选择包含数据的文件夹”
    .AllowMultiSelect=True
    如果.Show-1,则转到下一个代码
    Folderpath=.SelectedItems(1)
    以
    下一个代码:
    getFolder=Folderpath
    Set Flder=无
    设置mainWorkBook=ActiveWorkbook
    工作表(“工作表1”)。激活
    设置fso=CreateObject(“Scripting.FileSystemObject”)
    NoOfFiles=fso.getFolder(Folderpath).Files.Count
    设置listfiles=fso.getFolder(Folderpath).Files
    对于列表文件中的每个fls
    strCompFilePath=Folderpath&“\”和Trim(fls.Name)
    如果strCompFilePath为“”,则
    “//此处包括图像扩展\\
    如果(InStr(1,strCompFilePath,“jpg”,vbTextCompare)>1_
    或InStr(1,strCompFilePath,“jpeg”,vbTextCompare)>1_
    或InStr(1,strCompFilePath,“png”,vbTextCompare)>1)然后
    计数器=计数器+1
    Filename=fls.Name
    如果InStr(文件名“.”>0,则
    Filename=左(Filename,InStr(Filename,“.”-1)
    如果结束
    '将对齐设置为AlignCenter
    ActiveSheet.Columns(“A:BT”).VerticalAlignment=xlVAlignCenter
    '设置WrapText
    ActiveSheet.Range(“A:BT”)。选择
    有选择
    .WrapText=True
    以
    '插入文件名
    ActiveSheet.Range(“A”&计数器).Value=文件名
    '为29个文件名/图像设置列宽度
    范围(“A:B,D:G,I:L,N:Q,S:V,X:AA,AC:AF,AH:AK,AM:AP,AR:AU,AW:AZ,BB:BE,BG:BJ,BL:BO,BQ:BT”)。列宽=19
    '设置缩放图像的行高
    ActiveSheet.Range(“B”和计数器)。行高=70
    ActiveSheet.Range(“B”和计数器)。激活
    调用插入(strCompFilePath,计数器)
    激活工作表
    如果结束
    如果结束
    下一个
    'mainWorkBook.Save
    端接头
    函数插入(PicPath,计数器)
    'MsgBox PicPath
    使用ActiveSheet.Pictures.insert(PicPath)
    “//在此处更改图像大小\\
    带.形符
    .LockAspectRatio=msoTrue
    .宽度=50
    .高度=70
    以
    .Left=ActiveSheet.Range(“B”和计数器)。Left
    .Top=ActiveSheet.Range(“B”和计数器).Top
    .Placement=1
    .PrintObject=True
    以
    端函数
    
    您对“A”和“B”列进行了硬编码,其中列号更适合每10行循环一次并使列前进。我将一些格式从For循环内部移动到它上面,现在以数字形式引用列。您还需要说明AB中的1组,跳过C,DE和FG中的1组,跳过H,冲洗并重复。我在一组82个JPG上测试了下面对代码的修改,它似乎生成了一个如图所示的布局。该函数现在也接受第三个参数

    Option Explicit
    
    Sub EmbedImages()
    
        Dim mainWorkBook As Workbook
        Dim Flder As FileDialog
        Dim Folderpath, fStr, myPath, Filename, getFolder As String
        Dim fso, NoOfFiles, listfiles, fls, strCompFilePath
        Dim curRow As Long
        Dim curCol As Long
        Dim trips as Long
    
        curRow = 0
        curCol = 1
        trips = 1
        ' Get source images folder
        Set Flder = Application.FileDialog(msoFileDialogFolderPicker)
        With Flder
            .Title = "Select the folder containing data"
            .AllowMultiSelect = True
            If .Show <> -1 Then GoTo NextCode
            Folderpath = .SelectedItems(1)
        End With
    NextCode:
        getFolder = Folderpath
        Set Flder = Nothing
        Set mainWorkBook = ActiveWorkbook
        Sheets("Sheet1").Activate
        Set fso = CreateObject("Scripting.FileSystemObject")
        NoOfFiles = fso.getFolder(Folderpath).Files.Count
        Set listfiles = fso.getFolder(Folderpath).Files
    
        ' Set alignment as AlignCenter - moved this out of the loop
                ' Set ColumnWidth for 29 FileNames/Images
    
            ActiveSheet.Range("A:B,D:G,I:L,N:Q,S:V,X:AA,AC:AF,AH:AK,AM:_
            AP,AR:AU,AW:AZ,BB:BE,BG:BJ,BL:BO,BQ:BT").ColumnWidth = 19
            ActiveSheet.Columns("A:BT").VerticalAlignment = xlVAlignCenter
        ' Sets WrapText
        ActiveSheet.Range("A:BT").Select
        With Selection
            .WrapText = True
        End With
    
        For Each fls In listfiles
            strCompFilePath = Folderpath & "\" & Trim(fls.Name)
            If strCompFilePath <> "" Then
                ' // include image extensions here \\
                If (InStr(1, strCompFilePath, "jpg", vbTextCompare) > 1 _
                    Or InStr(1, strCompFilePath, "jpeg", vbTextCompare) > 1 _
                    Or InStr(1, strCompFilePath, "png", vbTextCompare) > 1) Then
                    curRow = curRow + 1
                    If curRow = 11 Then
                        curRow = 1
                        trips = trips + 1
                        If trips Mod (2) = 0 Then
                            curCol = curCol + 3
                        Else
                            curCol = curCol + 2
                        End If
                    End If
                    Filename = fls.Name
                    If InStr(Filename, ".") > 0 Then
                        Filename = Left(Filename, InStr(Filename, ".") - 1)
                    End If
                    ' Insert Filename
                    ActiveSheet.Cells(curRow, curCol).Value = Filename
                    ' Sets RowHeight for scaled image
                    ActiveSheet.Cells(curRow, curCol + 1).RowHeight = 70
                    ActiveSheet.Cells(curRow, curCol + 1).Activate
                    Call insert(strCompFilePath, curRow, curCol + 1)
                    ActiveSheet.Activate
                End If
            End If
        Next
        'mainWorkBook.Save
    End Sub
    
    Function insert(PicPath, thisRow, thisCol)
        'MsgBox PicPath
        With ActiveSheet.Pictures.insert(PicPath)
            '// change image sizes here \\
            With .ShapeRange
                .LockAspectRatio = msoTrue
                .Width = 50
                .Height = 70
            End With
            .Left = ActiveSheet.Cells(thisRow, thisCol).Left
            .Top = ActiveSheet.Cells(thisRow, thisCol).Top
            .Placement = 1
            .PrintObject = True
        End With
    End Function
    
    选项显式
    子图像()
    将工作簿设置为工作簿
    Dim Flder As FILE对话框
    Dim Folderpath、fStr、myPath、文件名、getFolder作为字符串
    Dim fso、NoOfFiles、listfiles、fls、strCompFilePath
    暗咖喱一样长
    长得一样暗
    旅途漫长
    curRow=0
    curCol=1
    行程=1
    '获取源图像文件夹
    Set Flder=Application.FileDialog(msoFileDialogFolderPicker)
    与弗尔德
    .Title=“选择包含数据的文件夹”
    .AllowMultiSelect=True
    如果.Show-1,则转到下一个代码
    Folderpath=.SelectedItems(1)
    以
    下一个代码:
    getFolder=Folderpath
    Set Flder=无
    设置mainWorkBook=ActiveWorkbook
    工作表(“工作表1”)。激活
    设置fso=CreateObject(“Scripting.FileSystemObject”)
    NoOfFiles=fso.getFolder(Folderpath).Files.Count
    设置listfiles=fso.getFolder(Folderpath).Files
    '将对齐设置为AlignCenter-将其移出循环
    '为29个文件名/图像设置列宽度
    范围(“A:B,D:G,I:L,N:Q,S:V,X:AA,AC:AF,AH:AK,AM:_
    AP,AR:AU,AW:AZ,BB:BE,BG:BJ,BL:BO,BQ:BT”)。柱宽=19
    ActiveSheet.Columns(“A:BT”).VerticalAlignment=xlVAlignCenter
    '设置WrapText
    ActiveSheet.Range(“A:BT”)。选择
    有选择
    .WrapText=True
    以
    对于列表文件中的每个fls
    strCompFilePath=Folderpath&“\”和Trim(fls.Name)
    如果strCompFilePath为“”,则
    “//此处包括图像扩展\\
    If(InStr(1,strCompFilePath,“jpg”,vbTextCompare)