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
效果图: