VBA is potentially the greatest desktop ‘hammer’ you have at your disposal. You can pop open any Office application, hit ALT+F11 and be writing code before Visual Studio will even be open on most machines.
For doing ad hoc work around the office, VBA allows you to do things that applications alone simply aren’t powerful enough to do. It seems like I’m always running into a situation where I need to get information about a bunch of files in a directory. Let’s say that you just need to retrieve the names of all files in a certain directory and write them to a column in an Excel spreadsheet.
Unfortunately, Excel doesn’t provide a native way to accomplish such a task. However, the task is reduced to less than 15 lines of code using VBA. The VBA Dir Function is great for iterating over a directory.
Understanding the Dir Function
This function returns a String value containing the name of a file, directory, or folder that matches a specified pattern or file attribute. If you’re working at the root level of a file structure, the Dir function will also return the volume label of a drive. returns the first file name that matches pathname parameter. The trick to looping through a directory is that calling Dir with no arguments returns any additional file names that match pathname. You can see an example of this in the code snippet below: the line with the comment, “get next entry” is where the Dir function will retrieve the next matching filename.
When Dir can no longer return a match, it returns a zero-length string (“”). If a zero-length string is returned, either a new pathname must be passed or an error occurs. You can change to a new pathname without retrieving all of the file names that match the current pathname.
NOTE: Do not nest Dir loops and don’t attempt to call the Dir function recursively. Calling Dir with the vbDirectory attribute does not continually return subdirectories. If you think you need to recurse directories, first consider whether you can copy the results of a simple Windows Search to a new directory to achieve your desired results.
Looping Over Files with Dir, Looping Through Files, Looping Through Directories…
The following VB code snippet is pretty easy to understand. There are three variables: strPath, strFile and x. strPath is used to pass the initial parameter to the Dir function. This simply tells Dir which directory to traverse. strFile is used to hold the file name as the code executes and continues to retrieve file names. x is used simply as a counter so that the correct cell in the Excel spreadsheet is updated with the correct filename (Dir doesn’t return files in any specific order, so you may need to sort them afterward).
Dim strPath As String
Dim strFile As String
Dim x As Integer strPath = “C:\temp\datajunction\XMLOUT\”
strFile = Dir(strPath)
Do While strFile <> “”
x = x + 1
Sheet1.Cells(x, 1) = strFile
strFile = Dir ‘ Get next entry.
Wise Man Says, “A Bird in Hand is Worth Two in Bush”
Quick Note: Recursing a Directory Function is Here
Popularity: 100% [?]