Addition to VBA code to run through Sub Folders

Bvendett4

New Member
Joined
Apr 10, 2018
Messages
24
Hello,
Please can some adapt the below code to allow the files to run through sub folders?
Code:
Sub Example1()

Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer

Sheet2.Activate

'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Get the folder object
Set objFolder = objFSO.GetFolder("G:\Shared\Health & Safety\5. Risk Assessments\1. Risk Assessments New")
i = 1

'loops through each file in the directory
For Each objFile In objFolder.Files
    'create hyperlink in appropriate cell
    ActiveSheet.Hyperlinks.Add Anchor:=ActiveSheet.Cells(i + 1, 2), Address:=objFile.Path, TextToDisplay:=objFile.Name
    'add date created to the right
    ActiveSheet.Cells(i + 1, 3).Value = objFile.DateLastModified
    i = i + 1
Next objFile

End Sub

Many Thanks
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hello,
Please accept my apologies but I cannot see the part that will help me, I am very much a beginner in VBA and the above code was already in place.
There have been sub folders added to the the original files and unfortunately these cannot be eliminated from the equation, therefore the code needs adapting.

Any further help would be appreciated.

Many Thanks
 
Upvote 0
Try
Code:
Sub LopFolder()


   Dim fso As FileSystemObject
   Dim f As folder, sf As folder
   Dim ofile As File
   Dim MyPath As String, MyFile As String, File As Workbook
   Dim i As Long
   
   i = 1
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.getfolder("G:\Shared\Health & Safety\5. Risk Assessments\1. Risk Assessments New")
   For Each ofile In f.Files
      ActiveSheet.Hyperlinks.Add Anchor:=ActiveSheet.Cells(i + 1, 2), Address:=ofile.Path, TextToDisplay:=ofile.Name
      ActiveSheet.Cells(i + 1, 3).Value = ofile.DateLastModified
      i = i + 1
   Next ofile
   For Each sf In f.subfolders
      For Each ofile In sf.Files
         ActiveSheet.Hyperlinks.Add Anchor:=ActiveSheet.Cells(i + 1, 2), Address:=ofile.Path, TextToDisplay:=ofile.Name
         ActiveSheet.Cells(i + 1, 3).Value = ofile.DateLastModified
         i = i + 1
      Next ofile
   Next sf


End Sub
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,391
Members
449,080
Latest member
Armadillos

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