Solved

List Files of a given directory in current worksheet using VBA

Posted on 2013-06-12
3
411 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 480 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 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 20 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

696 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