Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 623
Default Loop through all files in a folder

I need to process all the files in a particular folder.

Is there a "For all files in folder Do" construct in VBA?

--
Regards,
Fred
Please reply to newsgroup, not e-mail



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 227
Default Loop through all files in a folder

Hi Fred

Sub OpenWorkbooksInLocation()
Application.ScreenUpdating = False
Dim i as integer
With Application.FileSearch
..NewSearch
..LookIn = "C:\MyFolder\MySubfolder" 'Amend to suit
..SearchSubFolders = False
..FileName = "*.xls"
..Execute
For i = 1 To .FoundFiles.Count
Set wb = Workbooks.Open(FileName:=.FoundFiles(i))
'Do your stuff here
wb.Save
wb.Close
Next i
End With
Application.ScreenUpdating = True
End Sub

Or if it the intention only to list the files, then....

Sub ListWorkbooksInLocation()
Application.ScreenUpdating = False
Dim i As Integer
With Application.FileSearch
..NewSearch
..LookIn = "C:\MyFolder\MySubfolder" 'Amend to suit
..SearchSubFolders = False
..Filename = "*.xls"
..Execute
For i = 1 To .FoundFiles.Count
ActiveSheet.Cells(i, 1) = .FoundFiles(i)
Next i
End With
Application.ScreenUpdating = True
End Sub


--
XL2002
Regards

William



"Fred Smith" wrote in message
...
| I need to process all the files in a particular folder.
|
| Is there a "For all files in folder Do" construct in VBA?
|
| --
| Regards,
| Fred
| Please reply to newsgroup, not e-mail
|
|
|



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Loop through all files in a folder

Freed,

Here is some code that opens all workbooks in a folder

Sub Open(Folder As String)
Dim sFolder As String
Dim oWB As Workbook
Dim i As Long

With Application.FileSearch
.NewSearch
.LookIn = Folder
.SearchSubFolders = False
.Filename = "*.xls"
.FileType = msoFileTypeExcelWorkbooks
If .Execute() 0 Then
For i = 1 To .FoundFiles.Count
Set oWB = Workbooks.Open(Filename:=.FoundFiles(i))
'do your stuff here
oWB.Close SaveChanges:=False
Next i
Else
MsgBox "Folder " & sFolder & " contains no required files"
End If
End With

End Sub


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Fred Smith" wrote in message
...
I need to process all the files in a particular folder.

Is there a "For all files in folder Do" construct in VBA?

--
Regards,
Fred
Please reply to newsgroup, not e-mail





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Loop through all files in a folder

Be careful using the name "Open" for the macro. "Open" is a
reserved word in VBA.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Bob Phillips" wrote in
message ...
Freed,

Here is some code that opens all workbooks in a folder

Sub Open(Folder As String)
Dim sFolder As String
Dim oWB As Workbook
Dim i As Long

With Application.FileSearch
.NewSearch
.LookIn = Folder
.SearchSubFolders = False
.Filename = "*.xls"
.FileType = msoFileTypeExcelWorkbooks
If .Execute() 0 Then
For i = 1 To .FoundFiles.Count
Set oWB =

Workbooks.Open(Filename:=.FoundFiles(i))
'do your stuff here
oWB.Close SaveChanges:=False
Next i
Else
MsgBox "Folder " & sFolder & " contains no

required files"
End If
End With

End Sub


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Fred Smith" wrote in message
...
I need to process all the files in a particular folder.

Is there a "For all files in folder Do" construct in VBA?

--
Regards,
Fred
Please reply to newsgroup, not e-mail







  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 623
Default Loop through all files in a folder

Thanks so much for your help. .FoundFiles is exactly what I need.

--
Regards,
Fred
Please reply to newsgroup, not e-mail


"Bob Phillips" wrote in message
...
Freed,

Here is some code that opens all workbooks in a folder

Sub Open(Folder As String)
Dim sFolder As String
Dim oWB As Workbook
Dim i As Long

With Application.FileSearch
.NewSearch
.LookIn = Folder
.SearchSubFolders = False
.Filename = "*.xls"
.FileType = msoFileTypeExcelWorkbooks
If .Execute() 0 Then
For i = 1 To .FoundFiles.Count
Set oWB = Workbooks.Open(Filename:=.FoundFiles(i))
'do your stuff here
oWB.Close SaveChanges:=False
Next i
Else
MsgBox "Folder " & sFolder & " contains no required files"
End If
End With

End Sub


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Fred Smith" wrote in message
...
I need to process all the files in a particular folder.

Is there a "For all files in folder Do" construct in VBA?

--
Regards,
Fred
Please reply to newsgroup, not e-mail







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
Pulling pdf files from general folder to specific folder [email protected] Excel Discussion (Misc queries) 2 September 8th 09 09:41 PM
Loop through folder of workbooks and add rows FIRSTROUNDKO via OfficeKB.com Excel Worksheet Functions 0 August 10th 06 07:50 PM
Copying all files in a folder to new folder michaelberrier Excel Discussion (Misc queries) 2 June 20th 06 05:35 AM
how can I specific a folder with wildcard criteria and excel will import all the correct files in that folder? Raven Excel Discussion (Misc queries) 1 January 24th 06 03:28 PM
Loop through workbooks in a folder and return the value of cell M43 RockNRoll[_2_] Excel Programming 1 January 21st 04 07:46 PM


All times are GMT +1. The time now is 09:50 PM.

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"