Excel VBA file Traversal

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.