You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求修改VBA文件移动代码:实现从指定文件夹移动全类型文件至目标文件夹

Modify VBA Code to Move All File Types

Got it, let's tweak your existing VBA code to move every file type (instead of just .xlsx) from the source folder to the destination. Here's the adjusted version, plus a breakdown of the changes:

Updated Code

Sub MoveFiles() 
    Dim sourceFolderPath As String, destinationFolderPath As String 
    Dim FSO As Object, sourceFolder As Object, file As Object 
    Dim fileName As String, sourceFilePath As String, destinationFilePath As String 
    
    Application.ScreenUpdating = False 
    
    sourceFolderPath = "E:\Source" 
    destinationFolderPath = "E:\Destination" 
    
    Set FSO = CreateObject("Scripting.FileSystemObject") 
    Set sourceFolder = FSO.GetFolder(sourceFolderPath) 
    
    For Each file In sourceFolder.Files 
        fileName = file.Name 
        ' Removed the .xlsx filter - now all files are processed
        sourceFilePath = file.Path 
        destinationFilePath = destinationFolderPath & "\" & fileName 
        FSO.MoveFile Source:=sourceFilePath, Destination:=destinationFilePath 
    Next 
    
    Set sourceFolder = Nothing 
    Set FSO = Nothing 
    
    Application.ScreenUpdating = True ' Optional: Re-enable screen updating when done
End Sub

Key Changes

  • Removed the file type filter: The original code had an If InStr(fileName, ".xlsx") Then block that only moved Excel files. By removing this condition, every file in the source folder will be moved to the destination.
  • Added optional screen re-enabling: I added Application.ScreenUpdating = True at the end to restore Excel's default behavior once the operation finishes—this is a nice touch to avoid leaving Excel in a "frozen" looking state.

Bonus: If You Need to Exclude Specific File Types

If you ever want to move all files except certain types (e.g., skip .txt or .log files), you can add a simple exclusion check like this:

For Each file In sourceFolder.Files 
    fileName = file.Name 
    ' Skip .txt and .log files, move everything else
    If Not (InStr(fileName, ".txt") > 0 Or InStr(fileName, ".log") > 0) Then
        sourceFilePath = file.Path 
        destinationFilePath = destinationFolderPath & "\" & fileName 
        FSO.MoveFile Source:=sourceFilePath, Destination:=destinationFilePath 
    End If
Next 

内容的提问来源于stack exchange,提问作者Salman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 20:39:08