Vba批量新建文件夹并移动文件

Sub CreateFoldersAndCopyImages()
    Dim FolderPath As String
    Dim MainFolder As String
    Dim SubFolder As String
    Dim ImagePaths As Variant
    Dim ImagePath As Variant
    Dim FolderName As String
    Dim LastRow As Long
    Dim i As Long
    Dim fso As Object, Folder As Object
    Dim ImageFile As Object
      
    ' 设置文件夹路径和模板
    FolderPath = "D:\卓\D 商品图片\1月\0103\"
      
    ' 获取最后一行的行号
    LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
      
    ' 循环遍历每一行数据
    For i = 2 To 4 ' 假设数据从第二行开始
        
        Set fso = CreateObject("Scripting.FileSystemObject")
        
        MainFolder = ActiveSheet.Cells(i, 1).Value ' 获取主文件夹名称
        SubFolder = ActiveSheet.Cells(i, 2).Value ' 获取子文件夹名称
        
        ' 创建主文件夹和子文件夹
                
        If fso.FolderExists(FolderPath & MainFolder) = False Then
                MsgBox ("第" & i & "行:" & FolderPath & MainFolder & "的文件夹路径不存在")
                MkDir FolderPath & MainFolder
                MsgBox ("创建了文件夹路径" & FolderPath & MainFolder)
        End If
        If fso.FolderExists(FolderPath & MainFolder & "\" & SubFolder) = False Then
                MsgBox ("第" & i & "行:" & FolderPath & MainFolder & "\" & SubFolder & "的子文件夹路径不存在")
                MkDir FolderPath & MainFolder & "\" & SubFolder
                MsgBox ("创建了子文件夹路径" & FolderPath & MainFolder)
        End If
          
        ' 定义图片路径数组
        ImagePaths = Array(ActiveSheet.Cells(i, 3).Value, ActiveSheet.Cells(i, 4).Value, ActiveSheet.Cells(i, 5).Value)
        ' 循环遍历每张图片并复制到子文件夹
        For Each ImagePath In ImagePaths
            Set fso = CreateObject("Scripting.FileSystemObject")
            Set ImageFile = fso.GetFile(ImagePath)
            MsgBox (FolderPath & MainFolder & "\" & SubFolder & "\" & ImageFile.Name)
            ImageFile.Copy FolderPath & MainFolder & "\" & SubFolder & "\", True
            FileCopy FolderPath & MainFolder & "\" & SubFolder & "\" & ImageFile.Name, FolderPath & MainFolder & "\" & SubFolder & "\" & "826.jpg"
            
            Set ImageFile = Nothing
            Set fso = Nothing
        Next ImagePath
    Next i
    MsgBox "文件夹和图片创建完成!"
End Sub

在这里插入图片描述

效果图:
效果图

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值