Friday, September 20, 2013

List all the .XLSM files from folder and sub folder - VBA Excel


Public Sub TestListDir()
    Worksheets(1).Cells(2, 1).Activate
    Call listDir("C:\temp\", 1)
End Sub

Public Sub listDir(strPath As String, lngSheet As Long)
Dim strFn As String
Dim strDirList() As String
Dim lngArrayMax, x As Long
lngArrayMax = 0
strFn = Dir(strPath & "*.*", 23)
While strFn <> ""
    If strFn <> "." And strFn <> ".." Then
        If (GetAttr(strPath & strFn) And vbDirectory) = vbDirectory Then
            lngArrayMax = lngArrayMax + 1
            ReDim Preserve strDirList(lngArrayMax)
            strDirList(lngArrayMax) = strPath & strFn & "\"
            'Debug.Print strDirList(lngArrayMax)
            
        Else
            ActiveCell.Value = strPath & strFn
             If InStr(strFn, ".xlsm") > 0 Then Debug.Print strPath & strFn
            'if(strFn.Name.)
            
            
            Worksheets(lngSheet).Cells(ActiveCell.Row + 1, 1).Activate
        End If
    End If
    strFn = Dir()
Wend
If lngArrayMax <> 0 Then
    For x = 1 To lngArrayMax
        Call listDir(strDirList(x), lngSheet)
    Next
End If
End Sub

No comments: