Make file catalogs easily with VBA

  
Using VBA in Excel makes it easy to traverse each file in a folder, and can add each file name obtained to the cell to make a file directory. Example Description: Xiao Zhang is a film and television work enthusiast, collecting a large number of film and television works on the computer hard disk (Figure 1). He wants to catalog the file names of the video files in each category folder into an Excel worksheet. If a file name is copied and pasted into the worksheet, the workload is imaginable. Is there any good way to help Xiao Zhang solve this problem? Example analysis: We think that since different types of movies are stored in different folders, we can iterate through the movie files in each folder and get their file names, and then look up in different worksheets. If the worksheet already exists, do not add it, otherwise add a condition record to the corresponding worksheet. How to achieve the corresponding tasks? Please come with me! The process of traversing folders with VBA is mainly to write VBA code in Excel, traversing each file in the folder and getting the corresponding file name. When it comes to writing code, you may be a little dizzy. In fact, the program code is not so mysterious, here you can first enter or copy my code into your file, then look at my explanation, everything is OK! First, open Excel 2003, click on <; Tools & Rarr; Macro & Rarr; Visual Basic Editor & rdquo; menu, then the Microsoft Visual Basic program window will pop up, which is the VBA editor. In the VBA editor window, select the “insert →module" menu. At this point you can see the blank code editing area on the right side. We can try to enter the code in the figure (Figure 2). Code download address: http://iask.sina.com.cn/u/ish?uid=1704202287 In the above code, a total of two procedures are written, namely FileList and ml. The function of the FileList process is to traverse the files in the folder and its subfolders represented by the parameter Path, and create a worksheet in Excle based on the name of the subfolder. Among them, in the FileList process, the abbreviation of FileSystemObject is FSO, of course, our common name is FSO component. FSO, as UFO is as exciting and fascinating, of course, more is a joy to worry. I don't see a space service provider advertisement: 100MB space as long as 60RMB/year, support database, what support … … ask FSO, and immediately discourage. This component can detect and display the information distribution of the system drive; it can also create, change, move and delete folders, and can detect the existence of some given folder. If it exists, it can extract the information of the folder. , such as the name, the date that was created or last modified, and so on. FSO also makes it easy to handle files. Just as other components are built, FSO references must also establish connections. Through the statement: Set fso=CreateObject("Scripting.FileSystemObject"), the FSO component can be called by the fso variable, that is, the file or folder on the drive can be processed by the variable fso. Making easy-to-operate buttons For the convenience of operation, we can put the process we wrote above on a button. First, we select “View →Toolbar →Forms" menu in Excel, click “ button" on the "forms" toolbar, hold down the left mouse button and drag a button as needed . In the pop-up window of "Specify Macro", we select the process ml, and then exit after confirming. Finally, click on the button's label and modify the button's label to change it to “Generate Directory”. Finally, click on the “Generate Directory” button and you will find that the worksheet is named with a worksheet named after the movie title folder, and the corresponding directory is generated in each worksheet (Figure 3). Tip: Activating VBA Code To make these events, VBA code, work, we need to adjust the security of the macro. Select “Tools & Rarr; Macro & Rarr; Security", set the security of the macro to “low”. This article comes from [System Home] www.xp85.com
Copyright © Windows knowledge All Rights Reserved