On the day off, go to excel home to check what you can learn and help you with any questions. I have seen many posts asking for help in traversing files in a specific folder. Some friends said that the Excel version 03 has a filesearch object that can traverse files in the folder. In Version 07, there is no filesearch object and I do not know how to traverse the file. I think what version 03 can do, Version 07 will certainly do, or else it will not go backwards? So I searched for the object and canceled the filesearch object! However, scripting. FileSystemObject can be used as follows:
Sub main ()
Dim FS, fold, FLS, FL
Set FS = Createobject ("scripting. FileSystemObject ")
Set fold = FS. getfolder (Folderpath)'FolderpathIt refers to the folder path, which is string type. It can be replaced manually in practice.
Set FLS = fold. Files
For each fl in FLS
If instr (FL. Name, ". xls") <> 0 then' avoid opening non-Excel files
Workbooks. Open Fl. path' open the file
Statements'ProcessingCode
Workbooks (FL. Name). Close savechanges: = true 'close the file
End if
Next
End sub
The built-in Dir () function of Excel VBA can also be implemented as follows:
Sub main ()
Dim nm as string
Nm = Dir (Folderpath& "*. Xls *")'FolderpathIt refers to the folder path, which is string type. It can be replaced manually in practice.
Do While Len (Nm) <> 0
Workbooks. OpenFolderpath& Nm
Statements'Processing code
Workbooks (Nm). Close savechanges: = true
Nm = Dir ()
Loop
End sub
In summary, all programming ideas are oo-oriented. People are always thinking about what objects to solve the problem. I don't know if Oo is not all. It is just a structured encapsulation. In terms of the above two implementations, I think the Dir function is more efficient, because it is built-in and does not require external reference, and it can use wildcards. In addition, the FSO model is included in the script Library (scrrun. DLL), if the computer does not load the Script Type Library, FSO is not available, but it is undeniable that the FSO model provides a complete file processing structure, making the encoding more simple and intuitive.