View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Rowan Drummond[_3_] Rowan Drummond[_3_] is offline
external usenet poster
 
Posts: 414
Default Use Macro to Select All Files Within A Folder

Hi Theresa

This will loop through each excel file in a folder. The folder is
hardcoded but you could prompt for the folder using
Application.GetOpenFileName (see VBA help) or just an InputBox.

Sub OpnFiles()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("c:\Temp") 'change directory
For Each objFile In objFolder.Files
If objFile.Type = "Microsoft Excel Worksheet" Then
Workbooks.Open Filename:=objFolder.path & "\" & objFile.Name

'do whatever
Workbooks(objFile.Name).Close True 'saves changes
End If
Next

End Sub

Hope this helps
Rowan

Theresa wrote:
No sorry.....
in one folder there may be 20 files. I need to open these files run another
macro to automatically insert some calculations save and close each as the
macro runs. Then I want to move to another folder (separately) which may
contain 25 files and do the same thing.



"JakeyC" wrote:


I can't give you the exact code you need, but it would be a loop with a
similar command to:
Workbooks.Open FileName:= _
"C:\Documents and Settings\USER\My Documents\theName.xls"

where theName is a filename. However;

Opening over 800 files simultaneously will surely exceed your
computer's memory spec, if not crash before you reach it.

Did you mean open and close file 1 then open and close file 2 then open
and close file 3... then file 800+ ?