我有200個文件夾中的所有文件夾中都有不同的名稱。現在,每個名稱不同的文件夾都有一個宏Excel文件(.xlsm)。我試圖用一個單獨的文件一次編輯所有文件。代碼如下所示:編輯不同文件夾中的多個excel文件全部合併到一個文件夾中
Sub Button1_Click()
Dim wb As Workbook
Dim ws As Excel.Worksheet
Dim strPath As String
Dim strFile As String
'Get the directories
strPath = "C:\Users\generaluser\Desktop\testing main folder\"
strFile = Dir(strPath)
'Loop through the dirs
Do While strFile <> ""
'Open the workbook.
strFileName = Dir(strPath & strFile & "*.xlsm")
'Open the workbook.
Set wb = Workbooks.Open(Filename:=strPath & strFile & "\" & strFileName , ReadOnly:=False)
'Loop through the sheets.
Set ws = Application.Worksheets(1)
'Do whatever
ws.Range("A1").Interior.ColorIndex = 0
'Close the workbook
wb.Close SaveChanges:=True
'Move to the next dir.
strFile = Dir
Loop
End Sub
但這不起作用。我試過調整它,但無論我做什麼都不做或導致錯誤。有人可以幫助我讓這個代碼工作。 (另外:「測試主文件夾」是我的桌面上的文件夾,用於保存包含200個其他文件夾的.xlsm文件。)
[這個答案](http://stackoverflow.com/questions/22645347/loop-through-all-subfolders-using-vba)將幫助你瞭解如何遍歷子文件夾。 –
[循環通過用戶指定的根目錄中的子文件夾和文件]可能的重複(http://stackoverflow.com/questions/14245712/cycle-through-sub-folders-and-files-in-a-user -specified-root-directory) – Comintern