ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Loop through all files in a folder (https://www.excelbanter.com/excel-programming/300503-loop-through-all-files-folder.html)

Fred Smith

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




William[_2_]

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




Bob Phillips[_6_]

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






Chip Pearson

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








Fred Smith

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









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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com