List files in 4 specific folder

Access Beginner

Active Member
Joined
Nov 8, 2010
Messages
311
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have found code which will list files in a specific folder. Code attached.

I have 4 folders I want files listed in. At the moment I have repeated the code and change the location in the code. I was hoping someone could look at the following code and amend so it'll fit into one piece of code. The location of the list will remain as per the code. Location of folders and where list of files to be placed as below.

Folder 1
Location "R:\Test\Location\Data Request Documents\Debts\RegionalReports"
List Files on sheet "DataDebt" from cell H31

Folder 2
Location "R:\Test\Location\Data Request Documents\Populations\RegionalReports"
List Files on sheet "DataRequestPt" from cell H31

Folder 3
Location "R:\Test\Location\Data Request Documents\Accounts\ZoneReports"
List Files on sheet "Accounts" from cell H31

Folder 4
Location "R:\Test\Location\Data Request Documents\Participation\RegionalReports"
List Files on sheet "PartiData" from cell H31

Code:
Sub ListDebtFiles()
Application.ScreenUpdating = False
Dim oFSO As Object, oFSOFolder As Object, oFSOFile As Object
Dim LastRow As Long
Dim sFilePath As String, Path As String
Dim ar() As Variant
Dim i As Integer, kounter As Integer
Set oFSO = CreateObject("scripting.filesystemobject")
i = 1
kounter = 0
ChDrive "R:\"
ChDir "R:\Test\Location\Data Request Documents\Debts\RegionalReports\"
''''Select folder that contains files
With Application.FileDialog(msoFileDialogFolderPicker)
  .AllowMultiSelect = False
  .InitialFileName = "R:\Test\Location\Data Request Documents\Debts\RegionalReports\"
  .Title = "Please select a folder..."
  If .Show = 0 Then Exit Sub
  sFilePath = .SelectedItems(1) & "\"
End With
Sheets("DataDebt").Range("H31:H" & Sheets("DataDebt").Cells(Rows.Count, "H").End(xlUp).Row).ClearContents
Set oFSOFolder = oFSO.GetFolder(sFilePath)
kounter = oFSOFolder.Files.Count
If kounter > 0 Then
    ReDim ar(1 To kounter, 1 To 1)
Else
MsgBox "Sorry, no files in the selected folder."
    Exit Sub
End If
For Each oFSOFile In oFSOFolder.Files
    ar(i, 1) = oFSOFile.Path
    i = i + 1
Next oFSOFile
Sheets("DataDebt").Range("H31:H" & 31 + kounter - 1).Value = ar()
    
Set oFSOFile = Nothing
Set oFSOFolder = Nothing
Set oFSO = Nothing
Erase ar
ActiveSheet.Columns("H").AutoFit
Application.ScreenUpdating = True
MsgBox "There are " & kounter & " files. Debt reports have been created"
End Sub

Sub ListPopFiles()
Application.ScreenUpdating = False
Dim oFSO As Object, oFSOFolder As Object, oFSOFile As Object
Dim LastRow As Long
Dim sFilePath As String, Path As String
Dim ar() As Variant
Dim i As Integer, kounter As Integer
Set oFSO = CreateObject("scripting.filesystemobject")
i = 1
kounter = 0
ChDrive "R:\"
ChDir "R:\Test\Location\Data Request Documents\Populations\RegionalReports\"
''''Select folder that contains files
With Application.FileDialog(msoFileDialogFolderPicker)
  .AllowMultiSelect = False
  .InitialFileName = "R:\Test\Location\Data Request Documents\Populations\RegionalReports\"
  .Title = "Please select a folder..."
  If .Show = 0 Then Exit Sub
  sFilePath = .SelectedItems(1) & "\"
End With
Sheets("DataRequestPt").Range("H31:H" & Sheets("DataRequestPt").Cells(Rows.Count, "H").End(xlUp).Row).ClearContents
Set oFSOFolder = oFSO.GetFolder(sFilePath)
kounter = oFSOFolder.Files.Count
If kounter > 0 Then
    ReDim ar(1 To kounter, 1 To 1)
Else
MsgBox "Sorry, no files in the selected folder."
    Exit Sub
End If
For Each oFSOFile In oFSOFolder.Files
    ar(i, 1) = oFSOFile.Path
    i = i + 1
Next oFSOFile
Sheets("DataRequestPt").Range("H31:H" & 31 + kounter - 1).Value = ar()
    
Set oFSOFile = Nothing
Set oFSOFolder = Nothing
Set oFSO = Nothing
Erase ar
ActiveSheet.Columns("H").AutoFit
Application.ScreenUpdating = True
MsgBox "There are " & kounter & " files. Oopulation reports have been created"
End Sub
 
 
Sub ListAccountsFiles()
Application.ScreenUpdating = False
Dim oFSO As Object, oFSOFolder As Object, oFSOFile As Object
Dim LastRow As Long
Dim sFilePath As String, Path As String
Dim ar() As Variant
Dim i As Integer, kounter As Integer
Set oFSO = CreateObject("scripting.filesystemobject")
i = 1
kounter = 0
ChDrive "R:\"
ChDir "R:\Test\Location\Data Request Documents\Accounts\ZoneReports\"
''''Select folder that contains files
With Application.FileDialog(msoFileDialogFolderPicker)
  .AllowMultiSelect = False
  .InitialFileName = "R:\Test\Location\Data Request Documents\Accounts\ZoneReports\"
  .Title = "Please select a folder..."
  If .Show = 0 Then Exit Sub
  sFilePath = .SelectedItems(1) & "\"
End With
Sheets("Accounts").Range("H31:H" & Sheets("Accounts").Cells(Rows.Count, "H").End(xlUp).Row).ClearContents
Set oFSOFolder = oFSO.GetFolder(sFilePath)
kounter = oFSOFolder.Files.Count
If kounter > 0 Then
    ReDim ar(1 To kounter, 1 To 1)
Else
MsgBox "Sorry, no files in the selected folder."
    Exit Sub
End If
For Each oFSOFile In oFSOFolder.Files
    ar(i, 1) = oFSOFile.Path
    i = i + 1
Next oFSOFile
Sheets("Accounts").Range("H31:H" & 31 + kounter - 1).Value = ar()
    
Set oFSOFile = Nothing
Set oFSOFolder = Nothing
Set oFSO = Nothing
Erase ar
ActiveSheet.Columns("H").AutoFit
Application.ScreenUpdating = True
MsgBox "There are " & kounter & " files. Oopulation reports have been created"
End Sub
Sub ListPartiFiles()
Application.ScreenUpdating = False
Dim oFSO As Object, oFSOFolder As Object, oFSOFile As Object
Dim LastRow As Long
Dim sFilePath As String, Path As String
Dim ar() As Variant
Dim i As Integer, kounter As Integer
Set oFSO = CreateObject("scripting.filesystemobject")
i = 1
kounter = 0
ChDrive "R:\"
ChDir "R:\Test\Location\Data Request Documents\Participation\RegionalReports\"
''''Select folder that contains files
With Application.FileDialog(msoFileDialogFolderPicker)
  .AllowMultiSelect = False
  .InitialFileName = "R:\Test\Location\Data Request Documents\Participation\RegionalReports\"
  .Title = "Please select a folder..."
  If .Show = 0 Then Exit Sub
  sFilePath = .SelectedItems(1) & "\"
End With
Sheets("PartiData").Range("H31:H" & Sheets("PartiData").Cells(Rows.Count, "H").End(xlUp).Row).ClearContents
Set oFSOFolder = oFSO.GetFolder(sFilePath)
kounter = oFSOFolder.Files.Count
If kounter > 0 Then
    ReDim ar(1 To kounter, 1 To 1)
Else
MsgBox "Sorry, no files in the selected folder."
    Exit Sub
End If
For Each oFSOFile In oFSOFolder.Files
    ar(i, 1) = oFSOFile.Path
    i = i + 1
Next oFSOFile
Sheets("PartiData").Range("H31:H" & 31 + kounter - 1).Value = ar()
    
Set oFSOFile = Nothing
Set oFSOFolder = Nothing
Set oFSO = Nothing
Erase ar
ActiveSheet.Columns("H").AutoFit
Application.ScreenUpdating = True
MsgBox "There are " & kounter & " files. Population reports have been created"
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

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