Solved

List Files of a given directory in current worksheet using VBA

Posted on 2013-06-12
3
404 Views
Last Modified: 2013-06-12
Dear Experts:

the following macro lists all files from a given directory in Column A of the current excel worksheet.

Just the file names are listed. How is the code to be re-written if I would like to have the whole path listed?

Help is much appreciated. Thank you very much in advance.

Regards, Andreas


Sub List_Files()
'List Files from a given directory in Column A of current excel worksheet
Dim MyFolder As String
Dim MyFile As String
Dim a As Integer
MyFolder = "C:\Test\" ' <-- Change to your folder
MyFile = Dir(MyFolder & "*.*")
a = 0
Do While MyFile <> ""
    a = a + 1
    Cells(a, 1).Value = MyFile
    MyFile = Dir
Loop
MsgBox "Done."
End Sub

Open in new window

0
Comment
Question by:AndreasHermle
3 Comments
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 480 total points
Comment Utility
Sub List_Files()
'List Files from a given directory in Column A of current excel worksheet
Dim MyFolder As String
Dim MyFile As String
Dim a As Integer
MyFolder = "C:\Test\" ' <-- Change to your folder
MyFile = Dir(MyFolder & "*.*")
a = 0
Do While MyFile <> ""
    a = a + 1
    Cells(a, 1).Value = MyFolder  & MyFile
    MyFile = Dir
Loop
MsgBox "Done."
End Sub

Kevin
0
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 20 total points
Comment Utility
Andreas,

No points, please :)

You may want to check this out as well:
http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_25063189.html#a26340696

Patrick
0
 

Author Closing Comment

by:AndreasHermle
Comment Utility
Dear both, thank you very much for your great help.

Regards, Andreas

Patrick: 20 points are almost none!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now