?
Solved

List Files of a given directory in current worksheet using VBA

Posted on 2013-06-12
3
Medium Priority
?
413 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 1920 total points
ID: 39242633
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 93

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 80 total points
ID: 39242665
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
ID: 39242704
Dear both, thank you very much for your great help.

Regards, Andreas

Patrick: 20 points are almost none!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

741 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