Rahulwork1991
Rahulwork1991

Reputation: 3

Insert all File name in Excel

I need a code which would insert all file names saved in path to excel

i made a code for excel files only but i need to obtain all the file names.

Sub NameInFile()

    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(Range("F4").Value)
    iRow = 2
    On Error Resume Next
    For Each myfile In f.Files
        If myfile.Name Like "*.xls?" Then
            Cells(iRow, 40).Value = myfile.Name
            iRow = iRow + 1
            N = N + 1
        End If
    Next myfile
    Columns("AL").AutoFit
    
Range("D9").Interior.ColorIndex = 43

End Sub

Upvotes: 0

Views: 39

Answers (1)

shrivallabha.redij
shrivallabha.redij

Reputation: 5902

You need to drop If..end if condition from your code.

Your code should be as below, I have also removed redundant N variable which is not being used in the code.

Sub NameInFile()

Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(Range("F4").Value)
iRow = 2
On Error Resume Next
For Each myfile In f.Files
    '\\ If condition is removed
    Cells(iRow, 40).Value = myfile.Name
    iRow = iRow + 1
Next myfile
Columns("AL").AutoFit

Range("D9").Interior.ColorIndex = 43

End Sub

Upvotes: 1

Related Questions