跳到主要內容

如何在Excel中將特定文件從一個文件夾移動到另一個文件夾?

假設,我有一個大文件夾,其中包含各種文件,例如docx,jpg,xlsx等,如下面的屏幕快照所示。 現在,我想將某些特定的文件類型從文件夾移動到另一個新文件夾,而無需手動一個個地移動。 您有什麼好主意可以解決Excel中的此任務嗎?

使用VBA代碼將特定文件從一個文件夾移動到另一個文件夾


使用VBA代碼將特定文件從一個文件夾移動到另一個文件夾

若要將所有特定類型的文件從一個文件夾快速移動到另一個文件夾,以下VBA代碼可能會為您提供幫助:

1。 按住 ALT + F11 鍵打開 Microsoft Visual Basic for Applications 窗口。

2。 點擊 插入 > 模塊,然後將以下代碼粘貼到 模塊 窗口。

VBA代碼:將特定類型的文件從一個文件夾移動到另一文件夾:

Sub MoveFiles()
'Updateby Extendoffice
    Dim xFd As FileDialog
    Dim xTFile As String
    Dim xExtArr As Variant
    Dim xExt As Variant
    Dim xSPath As String
    Dim xDPath As String
    Dim xSFile As String
    Dim xCount As Long
    Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
    xFd.Title = "Please select the original folder:"
    If xFd.Show = -1 Then
        xSPath = xFd.SelectedItems(1)
    Else
        Exit Sub
    End If
    If Right(xSPath, 1) <> "\" Then xSPath = xSPath + "\"
    xFd.Title = "Please select the destination folder:"
    If xFd.Show = -1 Then
        xDPath = xFd.SelectedItems(1)
    Else
        Exit Sub
    End If
    If Right(xDPath, 1) <> "\" Then xDPath = xDPath + "\"
    xExtArr = Array("*.xlsx*", "*.jpg")
    For Each xExt In xExtArr
        xTFile = Dir(xSPath & xExt)
        Do While xTFile <> ""
            xSFile = xSPath & xTFile
            FileCopy xSFile, xDPath & xTFile
            Kill xSFile
            xTFile = Dir
            xCount = xCount + 1
        Loop
    Next
    MsgBox "Total number of moved files is: " & xCount, vbInformation, "Kutools for Excel"
End Sub

備註:在上面的代碼中,“* .xlsx *“”* .JPG在腳本中: xExtArr = Array(“ *。xlsx *”,“ * .jpg”) 是您要移動的文件類型,您可以將它們更改為其他文件類型,或根據需要添加其他文件類型。

3。 然後按 F5 鍵以運行此代碼,並彈出一個窗口,提醒您選擇要從中移動文件的原始文件夾,請參見屏幕截圖:

4。 然後點擊 OK,然後會彈出另一個窗口,請選擇要將文件移動到的目標文件夾,請參見屏幕截圖:

5。 然後點擊 OK,將彈出一個提示框,提醒您已移動了多少文件,將其關閉,您可以看到特定的jpg,xlsx文件已移動到指定的文件夾,請參見屏幕截圖:

最佳辦公生產力工具

🤖 Kutools 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行   |  生成代碼  |  建立自訂公式  |  分析數據並產生圖表  |  呼叫 Kutools 函數...
熱門特色: 尋找、突出顯示或識別重複項   |  刪除空白行   |  合併列或儲存格而不遺失數據   |   沒有公式的回合 ...
超級查詢: 多條件VLookup    多值VLookup  |   跨多個工作表的 VLookup   |   模糊查詢 ....
高級下拉列表: 快速建立下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
欄目經理: 新增特定數量的列  |  移動列  |  切換隱藏列的可見性狀態  |  比較範圍和列 ...
特色功能: 網格焦點   |  設計圖   |   大方程式酒吧    工作簿和工作表管理器   |  資源庫 (自動文字)   |  日期選擇器   |  合併工作表   |  加密/解密單元格    按清單發送電子郵件   |  超級濾鏡   |   特殊過濾器 (過濾粗體/斜體/刪除線...)...
前 15 個工具集12 文本 工具 (添加文本, 刪除字符,...)   |   50+ 圖表 類型 (甘特圖,...)   |   40+ 實用 公式 (根據生日計算年齡,...)   |   19 插入 工具 (插入二維碼, 從路徑插入圖片,...)   |   12 轉化 工具 (數字到單詞, 貨幣兌換,...)   |   7 合併與拆分 工具 (高級合併行, 分裂細胞,...)   |   ... 和更多

使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。  點擊此處獲取您最需要的功能...

產品描述


Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!
Comments (10)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
i get no popups after theselection screen
This comment was minimized by the moderator on the site
Hi,
Code works great for what I am doing. Thank you! How do you have it check if files exist in the destination folder and prompt the user to ask if they want to overwrite the files or save as another file name? I am working with .pdf files saving to the destination folder.
This comment was minimized by the moderator on the site
To move or copy files from a folder to another based on excel list, and destination (path)
on excel list. plz help...
This comment was minimized by the moderator on the site
Hello
To move or copy files from a folder to another based on excel list, the following article may help you!
This comment was minimized by the moderator on the site
Hi,
I need a help. Can we select few files and move them in to a separate folder by list in excel file?
thanks in advance
This comment was minimized by the moderator on the site
thanks a lot for your big help
This comment was minimized by the moderator on the site
Hello, Kumar,
To move or copy files from a folder to another based on excel list, the following article may help you!
https://www.extendoffice.com/documents/excel/4775-move-files-based-on-excel-list.html

Hope it can help you, thank you!
This comment was minimized by the moderator on the site
Can we select destinaton path in excel list on this macro. plz help.
This comment was minimized by the moderator on the site
Thank you very much, this is excellent. Really appreciate your help.
This comment was minimized by the moderator on the site
Hi

Thanks for helpful tip. I have around 5000 files listed on one excel file. I need to search all these files from the entire computer , if file names are matched then copy and paste in another folder
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations