这个问题不应该是复杂的。我有一个大文件夹,里面有200个单独的文件夹。现在每个文件夹中都有一个excel表。我想在控制文件夹(紧挨着200)的vba文件中有一些代码,它可以遍历200个文件夹并更改每个excel文件中的一位数据。我找到了目录东西和文件夹迭代,但我不能在这里和那里把它们合并在一起,我需要一些简单的帮助。
我的代码目前是:`Sub Button1_Click()
Dim wb As Workbook
Dim ws As Excel.Worksheet
Dim iIndex As Integer
Dim strPath As String
Dim strFile As String
'Get the directories
strPath = "C:\Users\generaluser\Desktop\testing main folder\"
strFile = Dir(strPath, vbDirectory)
'Loop through the dirs
Do While strFile <> ""
'Open the workbook.
strFileName = Dir(strPath & strFile & "New Microsoft Excel Worksheet.xlsm", vbDirectory)
'Open the workbook.
Set wb = Workbooks.Open(Filename:=strPath & strFile & "\" & strFileName, ReadOnly:=False)
'Loop through the sheets.
Set ws = Application.Worksheets(1)
'Do whatever
'Close the workbook
wb.Close SaveChanges:=True
'Move to the next dir.
strFile = Dir
Loop
End Sub `
请帮助@MatthewD
答案 0 :(得分:1)
由于你没有显示代码,它就是这样的。
Dim wb As Workbook
Dim ws As Excel.Worksheet
Dim iIndex As Integer
Dim strPath As String
Dim strFile As String
'Get the directories
strPath = "c:\temp\"
strFile = Dir(strPath, vbDirectory)
'Loop through the dirs
Do While strFile <> ""
'Open the workbook.
Set wb = Workbooks.Open(filename:=strPath & strFile & "\filename.xlsx", ReadOnly:=True)
'Loop through the sheets.
For iIndex = 1 To Application.Worksheets.count
Set ws = Application.Worksheets(iIndex)
'Do whatever
Next iIndex
'Close the workbook
wb.Close SaveChanges:=False
'Move to the next dir.
strFile = Dir
Loop
如果工作簿名称未知,则必须在目录中指定xlsx文件。
strFileName = Dir(strPath & strFile & "*.xlsx")
'Open the workbook.
Set wb = Workbooks.Open(filename:=strPath & strFile & "\" & strFileName , ReadOnly:=True)
答案 1 :(得分:0)
好的,这应该很容易。简单地以递归方式列出所有文件夹中的每个文件。下面的脚本将为您完成。
Sub ListAllFiles()
SearchForFiles "C:\Users\rshuell001\Desktop\YourFolder\", "writefilestosheet", "*.*", True, True
End Sub
Sub searchForFiles(ByVal DirToSearch As String, ByVal ProcToCall As String, _
Optional ByVal FileTypeToFind As String = "*.*", _
Optional ByVal SearchSubDir As Boolean = False, _
Optional ByVal FilesFirst As Boolean = False)
On Error GoTo ErrXIT
If Right(DirToSearch, 1) <> Application.PathSeparator Then _
DirToSearch = DirToSearch & Application.PathSeparator
If FilesFirst Then processFiles DirToSearch, ProcToCall, FileTypeToFind
If SearchSubDir Then processSubFolders DirToSearch, ProcToCall, _
FileTypeToFind, SearchSubDir, FilesFirst
If Not FilesFirst Then _
processFiles DirToSearch, ProcToCall, FileTypeToFind
Exit Sub
ErrXIT:
MsgBox "Fatal error: " & Err.Description & " (Code=" & Err.Number & ")"
Exit Sub
End Sub
Private Sub processFiles(ByVal DirToSearch As String, _
ByVal ProcToCall As String, _
ByVal FileTypeToFind As String)
Dim aFile As String
aFile = Dir(DirToSearch & FileTypeToFind)
Do While aFile <> ""
Application.Run ProcToCall, DirToSearch & aFile
aFile = Dir()
Loop
End Sub
Sub writeFilesToSheet(ByVal aFilename As String)
With ActiveSheet
.Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Value = aFilename
End With
End Sub
接下来,如您所知,您需要使用上述技术访问每个文件,打开每个文件,进行更改,保存并关闭文件。使用以下URL中描述的技术进行更改。
http://www.rondebruin.nl/win/s3/win010.htm
您必须稍微修改一下脚本,因为它会查找一个文件夹中的所有文件,您需要Ron的脚本来运行您使用第一个脚本创建的不同路径