View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Keith Willshaw Keith Willshaw is offline
external usenet poster
 
Posts: 170
Default loop through workbooks


"Rhonda" wrote in message
...
I need a macro to loop through a directory
(C:\MyDocuments) and identify files with the .bak
extension. Then open them one at a time because I need to
copy columns to a master. I found this macro, can it be
changed to my needs?


Not really, that routine loops through the worksheets in an open
workbook

You probably need to use the dir function to return a
list of the files in your directory and then use the workbook.Open
method

Something like

Dim myfilelist(1000) As String, myfilecount As Long
Dim myfile as string, xlbook as Workbook

myfile = Dir("C:MyDocuments\*.bak", vbNormal)

Do While myfile < ""
myfilelist(myfilecount) = mydirlist(i) & "\" & myfile
myfilecount = myfilecount + 1
myfile = Dir
Loop

For i = 0 To myfilecount
If myfilelist(i) < "" Then
Set xlbook = xWorkbooks.Open(myfilelist(i))

' Do Your Stuff
xlbook.Close
Set xlbook = Nothing
End If
Next i

Keith