macro to open files


Posted by Marty on December 14, 2001 5:30 AM

I would like to extract information from dailey files. How can I get a macro to open a file for one day, preform the task and open a file for the next day, perform the task and so on.



Posted by Tom Urtis on December 14, 2001 6:00 AM

Here's a way, to get you started

Try this code to get you started. Of course you would need to insert your "perform the task" code, and modify it for your actual file name and path. I put in a bunch of notes so you can see why each line does what it does.

Hope this helps:

Sub OpenFiles()
'Error code so you don't have to worry
'whether the DailyFile is open or closed
'at the time you run your macro, this way
On Error Resume Next
Windows("DailyFileName.xls").Activate
On Error GoTo 0
ChDir "C:\Your\File\Path"
Workbooks.Open Filename:="C:\Your\File\Path\DailyFileName.xls"

'YOUR MACRO GOES HERE

'Then when you are done, save and close the DailyFile
'Note, activate it first, in case your macro's
'last step involves a different workbook:
Windows("DailyFileName.xls").Activate
ActiveWorkbook.Save
ActiveWorkbook.Close
End Sub


HTH

Tom Urtis