Tuesday, March 18, 2014

Excel Macro for getting file list in a folder


Mesmerized of how to get list of filenames in folder, try this out.
Just follow these simple steps:
File – Option – Customize the Ribbon –Main Tabs –
Click on Developer - Click on Add.
Now click on the home, there you can find Developer option in the ribbon.

Now click on Visual Basic.
Click on insert - Module.
Paste this code below:

Option Explicit
Sub GetFileNames()
Dim xRow As Long
Dim xDirect$, xFname$, InitialFoldr$
InitialFoldr$ = "C:\" '<<< Startup folder to begin searching from
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = Application.DefaultFilePath & "\"
.Title = "Please select a folder to list Files from"
.InitialFileName = InitialFoldr$
.Show
If .SelectedItems.Count <> 0 Then
xDirect$ = .SelectedItems(1) & "\"
xFname$ = Dir(xDirect$, 7)
Do While xFname$ <> ""
ActiveCell.Offset(xRow) = xFname$
xRow = xRow + 1
xFname$ = Dir
Loop
End If
End With
End Sub


Now click on Run.

Your Macro is now ready.
Now close the file, save as type Excelmacro-enabled workbook.
Now again open it.
Go to developer tab.
Click on macro.
Select your macro and press run.

Download Excel.



Thank You.









No comments :

Post a Comment