【发布时间】:2016-07-18 21:26:11
【问题描述】:
我在一个文件夹中有 200 个不同名称的文件夹。现在,每个具有不同名称的文件夹都有一个宏 excel 文件 (.xlsm)。我正在尝试使用单独的文件一次编辑所有文件。代码如下:
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 文件。)
【问题讨论】:
-
this answer 将帮助您了解如何遍历子文件夹。
标签: vba excel subdirectory