Open file within multiple subfolders and loop

brad999

New Member
Joined
Feb 23, 2014
Messages
15
Evening,

I have the following code which opens a file, within a subfolder of the activeworkbookpath, with the filename stored in cell A70. However, the file could be stored within 1 subfolder, or within multiple subfolders, so I need the code to look through all subfolders of the activeworkbookpath.

Also, ideally, it would be great if a loop could be in place that would open the filename at A70, extract the relevant information, then open the filename at A71 and extract relevant information and repeat until cell A# is blank.

Is this possible?? Thanks in advance!!


Code:
Dim simWbk As Excel.Workbook
Dim simSht As Worksheet


Set simWbk = ActiveWorkbook
Set simSht = ActiveSheet






Dim strFilePath As String
Dim strFileName As String
Dim objFolder As Object


strFilePath = ActiveWorkbook.Path & "\"
strFileName = strFilePath & Sheets("Temp_Data").Range("A70")


For Each objFolder In CreateObject("Scripting.FileSystemObject"). _
        GetFolder(strFilePath).SubFolders
  If Len(Dir(objFolder.Path & "\" & Sheets("Temp_Data").Range("A70"))) > 0 Then
    With Workbooks.Open(objFolder.Path & "\" & Sheets("Temp_Data").Range("A70"))
      .Sheets(1).Range("A1:W59").Copy
      ThisWorkbook.Sheets("Temp_Data").Range("A1").PasteSpecial xlPasteValues
      ThisWorkbook.Sheets("Temp_Data").Range("A64").Value = .BuiltinDocumentProperties("Last Author").Value
      Application.DisplayAlerts = False
      .Close False
    End With
    Sheets("Temp_Data").Range("A63").Value = Mid$(strFileName, InStrRev(strFileName, "\") + 1)
  End If
Next objFolder
Application.ScreenUpdating = True
 
Last edited:

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Forum statistics

Threads
1,216,725
Messages
6,132,347
Members
449,719
Latest member
excel4mac

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top