Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
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


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 170
Default loop through workbooks


"Rhonda" wrote in message
...
What should dirlist be?


Sorry I cut this from a more complex example which
looped through directories too and mydirlist was the
folder path, I thought I cut that out but missed that one

Here's the fixed version

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) = 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


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 24
Default loop through workbooks

Thanks Keith, appreciate the help!!


-----Original Message-----

"Rhonda" wrote in message
...
What should dirlist be?


Sorry I cut this from a more complex example which
looped through directories too and mydirlist was the
folder path, I thought I cut that out but missed that

one

Here's the fixed version

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) = 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


.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find loop doesn't loop JSnow Excel Discussion (Misc queries) 2 June 24th 09 08:28 PM
Copy/ move selected data from workbooks to seperate worksheets or workbooks Positive Excel Worksheet Functions 1 August 30th 07 04:54 PM
Loop through folder of workbooks and add rows FIRSTROUNDKO via OfficeKB.com Excel Worksheet Functions 0 August 10th 06 07:50 PM
loop through workbooks Ron de Bruin Excel Programming 1 September 18th 03 02:53 PM
Can I have a loop to open a set of workbooks get some data, close it one a time. wellie Excel Programming 2 July 9th 03 04:58 AM


All times are GMT +1. The time now is 05:05 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"