下載后我需要幫助將多個檔案復制到一個檔案夾中。
下載后的檔案默認重命名為:
- Branch_A.xlsx
- Branch_A (1).xlsx
- Branch_A (2).xlsx
我通常使用這里的代碼,但此代碼僅將一個檔案復制到檔案夾中。我需要代碼可以捕獲單詞“Branch_A”然后計算檔案并復制到檔案夾 Branch_A。計數檔案是修復 3 它將來 4..5..6..
Sub down1()
'RENAME FILE DOWNLOAD BY BRANCHES
Dim Ws As Worksheet
Dim FromPath As String
Dim ToPath As String
Set Ws = ThisWorkbook.Worksheets("Path_Down1")
'FROM DOWNLOAD - C:\Users\Downloads\
FromPath = Ws.Range("E11").Value
'TO FOLDER - D:\Inbound\Branch_A\
ToPath = Ws.Range("F11").Value
Ws.Activate
FileCopy FromPath, ToPath
End Sub
我搜索了幾個網站,發現代碼可以根據擴展名計算檔案,但我不知道如何編輯以按名稱計數并復制到檔案夾。這里的示例代碼:
Sub sample()
Dim FolderPath As String, path As String, count As Integer
FolderPath = "C:\Documents and Settings\Santosh\Desktop"
path = FolderPath & "\*.xls"
Filename = Dir(path)
Do While Filename <> ""
count = count 1
Filename = Dir()
Loop
Range("Q8").Value = count
'MsgBox count & " : files found in folder"
End Sub
謝謝你的幫助。
uj5u.com熱心網友回復:
下面的代碼是您正在尋找的東西嗎?這就是我能想出的對我有意義的東西。如果不是,請提供有關問題所在的更多資訊。
Sub Down1()
Dim FromFolder As String, ToFolder As String
Dim FromPath As String, ToPath As String, ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Path_Down1")
FromFolder = ws.Range("E11").Value
ToFolder = ws.Range("F11").Value
Filename = Dir(FromFolder & "\*.xlsx")
Do While Filename <> ""
If InStr(Filename, "Branch_A") > 0 Then
FromPath = FromFolder & "\" & Filename
ToPath = ToFolder & "\" & Filename
FileCopy FromPath, ToPath
End If
Filename = Dir()
Loop
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/458964.html
標籤:vba
上一篇:如何處理VBA字串中的多個空格?
下一篇:計算每行excel的時間
