Solved

Excel VBA process files in directory recursively

Posted on 2013-01-29
3
689 Views
Last Modified: 2013-02-01
Hi,

I'm looking to borrow some code from someone's extensive Excel macro library.
I just need a skeleton that does the following:

Access a directory (hard coded, say "C:\temp")
For each Excel file in the directory (*.xls, *.xlsx?):
    Open file
    Starting at A1, place the file name.
    On the same row, copy a cell, say Sheet1!A1
    Close file

Thank you in advance.

Regards,
Richard
0
Comment
Question by:cyberkiwi
  • 2
3 Comments
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
ID: 38830729
Use something like this to find all the files
http://social.msdn.microsoft.com/Forums/en-US/exceldev/thread/1c51fccb-94f3-4147-9bcc-ad564cf33f87
Where you see the ... do something like this
ThisWorkbook.Worksheets("Sheet1").Range("A"&i).Value = sWrk.Name
ThisWorkbook.Worksheets("Sheet1").Range("B"&i).Value = sWrk.Worksheets("Sheet1").Range("A1")

Open in new window

0
 
LVL 58

Author Comment

by:cyberkiwi
ID: 38830790
Thanks Tommy.

That's what I was looking for, but I wanted to check if anyone has an industrial version of such a script to handle files Excel files, which could fire events (.xls + workbook_open), or that could be password protected for opening, or is already open by another Excel instance or the current one etc.
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 38830880
Hah. That's what I get when I don't read the author name before I answer the question.

Password stuff just gets passed to the Open method.
If you set the read-only flag on the Open method, it should handle the "already open somewhere else" case.
If the file is already open in the current Excel instance, you would have to get the data from the currently open one. If a different file with the same name is open in the current instance, then I think you're out of luck without closing it first.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
A short article about problems I had with the new location API and permissions in Marshmallow
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 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

21 Experts available now in Live!

Get 1:1 Help Now