File names to excel from respective folders

harinsh

Active Member
Joined
Feb 7, 2012
Messages
273
Hi All,


I am working on some automation and need your help in vba code to get excel file names from respective folders......for example ..I have 10 different folders and need the file names those were inside the folder to excel cell starting from A1, A2, A3...so on...


if I get the complete path it would even really help me.....


Thanks in advance...
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
This code will put the file name in column A and the full path in column B. You'll have to put the folder paths in the folderlist array in the first sub.

Code:
Option Explicit

Dim fs, f1, f, fc
Dim n As String
Dim r As Integer, c As Integer
Dim folderspec As String


Sub folderloop()
Dim folderlist() As String
Dim x As Integer


Worksheets(1).Columns("A:B").ClearContents


folderlist = Split("C:\Users\username\folder1,C:\Users\username\folder2,C:\Users\username\folder3,C:\Users\username\folder4", ",")


r = 2


For x = 0 To 9
    folderspec = folderlist(x)
    listFiles
Next x


End Sub


Sub listFiles()
    With Worksheets(1)
        .Range("A1").Value = "File Name"
        .Range("B1").Value = "Full Path"
        .Range("A1:B1").Font.Bold = True
    End With
            
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(folderspec)
    Set fc = f.Files
    
    For Each f1 In fc
        If fs.GetExtensionName(f1) = "xls" Or fs.GetExtensionName(f1) = "xlsm" Then
            Sheets(1).Cells(r, 1) = fs.GetBaseName(f1)
            Sheets(1).Cells(r, 2) = f1
            r = r + 1
        End If
    Next
End Sub

Regards,

CJ
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,141
Members
453,021
Latest member
Justyna P

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