Solved

Excel VBA process files in directory recursively

Posted on 2013-01-29
3
700 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
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…

930 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

19 Experts available now in Live!

Get 1:1 Help Now