bring files from folders and subfolders to multiple columns based on folders names

tubrak

Board Regular
Joined
May 30, 2021
Messages
218
Office Version
  1. 2019
Platform
  1. Windows
hi

I need macro to brings all files from folder and subfolders to multiple columns so my directory isle "C:\Users\mm\Desktop\FILES" it contains many subfolders FILE1 up to FILE 6 and it's increasable and each subfolder contain files are different in extensions so it should create the headers based on subfolders names and bring the files for each column separately and hyperlink to open them
this should result after run macro
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi, a VBA demonstration for starters to paste to the worksheet module :​
VBA Code:
Function DirList(SCAN$, Optional FOLD$, Optional ATTR As VbFileAttribute = vbNormal) As String()
         Dim B%, D$, F$, T$(), U&
    With Application
        If FOLD > "" Then
            If Right(FOLD, 1) <> .PathSeparator And Left(SCAN, 1) <> .PathSeparator Then FOLD = FOLD & .PathSeparator
            D = FOLD
        Else
            D = Left$(SCAN, InStrRev(SCAN, .PathSeparator))
        End If
    End With
        If SCAN = "." Then SCAN = "*."
        On Error Resume Next
        F = Dir(FOLD & SCAN, ATTR)
    Do Until F = ""
        If ATTR And vbDirectory Then B = Right(F, 1) = "." Or (GetAttr(D & F) And vbDirectory) = 0
        If B = 0 Then U = U + 1: ReDim Preserve T(1 To U): T(U) = FOLD & F
        F = Dir
    Loop
         DirList = IIf(U, T, Split(""))
End Function

Sub Demo1()
  Const M = " Scanning …", P = "C:\Users\mm\Desktop\FILES\", S = "\"
    Dim D$(), C%, H$(), R&
        UsedRange.Clear
        [A1].Value = M
        Application.ScreenUpdating = False
        D = DirList(P, , vbDirectory)
    For C = 1 To UBound(D)
        Cells(C).Value2 = D(C)
        H = DirList(P & D(C) & S)
    For R = 1 To UBound(H)
        Hyperlinks.Add Cells(R + 1, C), P & D(C) & S & H(R), , , H(R)
    Next R, C
        If [A1].Text = M Then [A1] = " Nothing !": Beep Else UsedRange.Columns.AutoFit
        Application.ScreenUpdating = True
End Sub
 
Upvote 0
gives error object required
VBA Code:
UsedRange.Clear
 
Upvote 0
Your bad as « solution belongs to good enough readers » so paste the demonstration where it must be just following the dark red direction above the code !​
 
Upvote 0
not my bad I put the whole code in worksheet module
1.PNG
 
Upvote 0
Yes your bad as this error comes only when the code is not located in a worksheet module like in your picture where it's a general / standard module ‼ :rolleyes:
So just open above the worksheet module of 'sheet1' or 'sheet2' …​
 
Upvote 0
forgive me how code works I run this Sub Demo1() but shows in cell A1 " Nothing !"
 
Upvote 0
Hi all,
here's my attempt, copy the code in a standard module, adapt sheet name and path if needed.
VBA Code:
Sub Test()
    
    Dim oFSO As Object
    Dim folder As Object
    Dim subfolders As Object
    Dim xFile As Object
    Dim Wks As Worksheet
    Dim rowIndex As Long
    Dim Col As Integer
    
    Col = 1
    rowIndex = 2
    
    Set Wks = ThisWorkbook.Sheets("Sheet1")        '==>> TO ADAPT
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set folder = oFSO.GetFolder("C:\Users\mm\Desktop\FILES")        '==>> TO ADAPT
    Set subfolders = folder.subfolders
    
    Application.ScreenUpdating = FALSE
    
    Wks.UsedRange.Clear
    
    For Each subfolders In folder.subfolders
        
        Wks.Cells(1, Col).Value = subfolders.Name
        
        For Each xFile In subfolders.Files
            Application.ActiveSheet.Cells(rowIndex, Col).Formula = xFile.Name
            rowIndex = rowIndex + 1
        Next xFile
        
        Col = Col + 1
        rowIndex = 2
    Next subfolders
    
    Wks.Columns.AutoFit
    
    Application.ScreenUpdating = TRUE
    
    Set oFSO = Nothing
    Set folder = Nothing
    Set subfolders = Nothing
    
End Sub
 
Upvote 0
@Sequoyah thanks you ! you barley did it just it remains some things , hyperlink the files to I can open them
 
Upvote 0
Hi *tubrak,
thanks for your feedback. I apologize for not reading your request carefully, add this code after the line

VBA Code:
Application.ActiveSheet.Cells(rowIndex, Col).Formula = xFile.Name

VBA Code:
  With ActiveCell
    .Hyperlinks.Add Anchor:=Cells(rowIndex, Col), Address:=subfolders & "\" & xFile.Name
  End With
 
Upvote 0

Forum statistics

Threads
1,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

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