Forum

Looping thru Differ...
 
Notifications
Clear all

Looping thru Different folders

5 Posts
2 Users
0 Reactions
98 Views
(@Anonymous)
Posts: 0
New Member Guest
 

I am trying to loop through about 50 sub-folders with excel files.

I need to find excel files in those folders that contain a header 'Name'

Can someone help me write the VBA code to achieve this.

 

Thank you

 
Posted : 13/08/2016 2:35 pm
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi ruzvmun,

Not sure what you mean by a header 'Name' - a column in the files with a particular name?  Or you're actually looking for the word 'Name'?  Or the files themselves have a specific name you are looking for?

If you can supply sample files to illustrate what you are looking for that would help.

If you are just looking for files with particular names, could you just use Search in Windows Explorer?

Have you looked at this blog post which lists files in subfolders ?

Regards

Phil

 
Posted : 15/08/2016 10:58 pm
(@Anonymous)
Posts: 0
New Member Guest
 

Hie Phil,

So basically I have a folder that has 20 subfolders. In those subfolders are excel files that contain customer information. What I am trying to do is locate a file within those folders that contains SS#, lets just say the column name is 'Social Security Number'. Does this make sense?

So far I have gotten as far as looping through the folders and listing the files in each of them.

 

Thanks for your help

 

Below is the code.

Sub GetExcelFiles()

    Dim FileSystem As Object
    Dim HostFolder As String
    Dim row As Integer
   
    row = 1
    HostFolder = "Main Folder Location"
    Set FileSystem = CreateObject("Scripting.FileSystemObject")
    DoFolder FileSystem.GetFolder(HostFolder), row

End Sub
Sub DoFolder(folder, row)
    Dim subfolder As Object
    Dim fileExt As String
    For Each subfolder In folder.SubFolders
        Cells(row, 1).Select
        ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:= _
        subfolder.path, TextToDisplay:= _
        subfolder.path
        row = row + 1
        DoFolder subfolder, row
    Next
    Dim file
    For Each file In folder.Files
        fileExt = Mid(file.Name, InStr(file.Name, ".") + 1, 100)
        If fileExt = "xls" Then
            Cells(row, 2).Select
             ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:= _
             file.path, TextToDisplay:= _
             file.Name
            row = row + 1
        End If
    Next
End Sub

 
Posted : 18/08/2016 4:26 pm
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi,

So you want to look inside each Excel file for a column named "Social Security Number" ?

Or are you looking for an actual number?

How many sheets in these files?  If you can provide sample workbooks that would help

Regards

Phil

 
Posted : 18/08/2016 10:33 pm
(@Anonymous)
Posts: 0
New Member Guest
 

Phil

Yes, I want to look inside each file for a column named 'SSN'.

One sheet per file.

I attached an example of what the files look like.

 
Posted : 19/08/2016 11:47 am
Share: