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
    评论
### 回答1: 使用VBA打印文件夹下的所有文件,可以通过FileSystemObject对象和Folder对象来实现。具体步骤如下: 1. 首先,需要引用Microsoft Scripting Runtime库,以便使用FileSystemObject对象。在VBA编辑器中,点击"工具" -> "引用",勾选"Microsoft Scripting Runtime"。 2. 声明并创建FileSystemObject对象和Folder对象,用于操作文件夹和文件。 3. 使用GetFolder方法指定文件夹路径,创建Folder对象。 4. 使用Folder对象的Files属性获取文件夹下的所有文件集合。 5. 使用For Each循环遍历文件集合中的每个文件。 6. 对于每个文件,可以使用PrintOut方法来实现打印。可以根据需要设置打印属性,如打印份数、打印机名称等。 一个简单的示例代码如下: ``` Sub PrintFilesInFolder() Dim fso As FileSystemObject Dim folderPath As String Dim folder As Folder Dim file As File '指定文件夹路径 folderPath = "C:\TestFolder" '创建文件系统对象和文件夹对象 Set fso = New FileSystemObject Set folder = fso.GetFolder(folderPath) '遍历文件夹中的每个文件 For Each file In folder.Files '打印文件 file.PrintOut Next file '释放对象 Set file = Nothing Set folder = Nothing Set fso = Nothing MsgBox "打印完成!" End Sub ``` 以上代码将打印指定文件夹中的所有文件。你可以根据实际需求修改文件夹路径和其他打印属性。 ### 回答2: 在VBA中,我们可以使用FileSystemObject对象来实现打印文件夹下所有文件的功能。 首先,我们需要引入"Microsoft Scripting Runtime"库。在VBA编辑器中,点击工具 -> 引用,然后勾选"Microsoft Scripting Runtime",点击确定。 下面是一个示例代码,演示如何打印文件夹下所有文件文件名: ```vba Sub 打印文件夹下所有文件() Dim FSO As New FileSystemObject Dim 文件夹路径 As String Dim 文件夹 As Folder Dim 文件 As File ' 设置文件夹路径 文件夹路径 = "C:\文件夹的路径" ' 获取文件夹对象 Set 文件夹 = FSO.GetFolder(文件夹路径) ' 遍历文件夹下所有文件 For Each 文件 In 文件夹.Files Debug.Print 文件.Name Next 文件 ' 释放对象 Set 文件夹 = Nothing Set FSO = Nothing End Sub ``` 请将"文件夹的路径"替换为你想要打印文件文件夹路径。运行该VBA代码后,文件夹下所有文件文件名将以文本形式输出到"立即窗口"。你可以通过按下"Ctrl + G"快捷键打开"立即窗口"。 注意:在使用FileSystemObject对象之前,确保你已经引入了正确的库。如果你还没有引入"Microsoft Scripting Runtime"库,请按照前面的步骤进行引入。 ### 回答3: 在VBA中,我们可以使用FileSystemObject对象来打印文件夹下所有的文件。下面是一个简单的代码示例: ```vba Sub PrintFilesInFolder() Dim fso As Object Dim folderPath As String Dim folder As Object Dim file As Object ' 设置文件夹路径 folderPath = "C:\YourFolderPath" ' 创建FileSystemObject对象 Set fso = CreateObject("Scripting.FileSystemObject") ' 获取文件夹对象 Set folder = fso.GetFolder(folderPath) ' 遍历文件夹下的所有文件 For Each file In folder.Files ' 打印文件名 Debug.Print file.Name Next file ' 清除对象变量 Set file = Nothing Set folder = Nothing Set fso = Nothing End Sub ``` 上述代码中,我们首先声明了所需的变量,包括`fso`(FileSystemObject对象),`folderPath`(文件夹路径),`folder`(文件夹对象),`file`(文件对象)。然后我们使用`CreateObject`函数创建了一个`fso`对象,并使用`GetFolder`方法获取了指定路径下的文件夹对象。接着我们使用`For Each`循环来遍历文件夹下的所有文件,通过`Debug.Print`语句打印文件名。最后,我们清除了所有对象变量。请注意替换`folderPath`为实际的文件夹路径。 使用上述代码,您可以打印出指定文件夹下的所有文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值