我有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