Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Ben Ben is offline
external usenet poster
 
Posts: 509
Default Cycling through all worbooks in a folder

I would like to perform a piece of code on every workbook in a folder and
then close it. What would be the best way please. Thank you
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 400
Default Cycling through all worbooks in a folder

This code will give you a starting point:

Sub aa()
Set fso =
CreateObject("Scripting.FileSystemObject").GetFold er("c:\OurFiles\")
For Each file In fso.Files
If file.Type = "Microsoft Excel Worksheet" Then
With file
' do what you want with this file
End With
End If
Next
Set fso = Nothing
End Sub

Note: Replace "C:\OURFILES" by your folder; this code does not recurse the
foldre tree if it has one.

"Ben" wrote:

I would like to perform a piece of code on every workbook in a folder and
then close it. What would be the best way please. Thank you

  #3   Report Post  
Posted to microsoft.public.excel.programming
Ben Ben is offline
external usenet poster
 
Posts: 509
Default Cycling through all worbooks in a folder



"AA2e72E" wrote:

This code will give you a starting point:

Sub aa()
Set fso =
CreateObject("Scripting.FileSystemObject").GetFold er("c:\OurFiles\")
For Each file In fso.Files
If file.Type = "Microsoft Excel Worksheet" Then
With file
' do what you want with this file
End With
End If
Next
Set fso = Nothing
End Sub

Note: Replace "C:\OURFILES" by your folder; this code does not recurse the
foldre tree if it has one.

"Ben" wrote:

I would like to perform a piece of code on every workbook in a folder and
then close it. What would be the best way please. Thank you

  #4   Report Post  
Posted to microsoft.public.excel.programming
Ben Ben is offline
external usenet poster
 
Posts: 509
Default Cycling through all worbooks in a folder

That's great but how would I sequentially open the files then perform the
code then close the file

"Ben" wrote:



"AA2e72E" wrote:

This code will give you a starting point:

Sub aa()
Set fso =
CreateObject("Scripting.FileSystemObject").GetFold er("c:\OurFiles\")
For Each file In fso.Files
If file.Type = "Microsoft Excel Worksheet" Then
With file
' do what you want with this file
End With
End If
Next
Set fso = Nothing
End Sub

Note: Replace "C:\OURFILES" by your folder; this code does not recurse the
foldre tree if it has one.

"Ben" wrote:

I would like to perform a piece of code on every workbook in a folder and
then close it. What would be the best way please. Thank you

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Cycling through all worbooks in a folder

Hi Ben,

Maybe you can adapt this to your needs -

Option Explicit

Function FilesToCol(sPath As String, c As Collection) As Long
Dim sFile As String

sFile = Dir(sPath & "\*.xls")
Do While Len(sFile)
c.Add sFile
sFile = Dir()
Loop
FilesToCol = c.Count

End Function

Sub Test()
Dim bWasClosed As Boolean
Dim sFolder As String
Dim col As Collection
Dim i As Long
Dim wb As Workbook

Set col = New Collection

sFolder = "C:\Temp"

If FilesToCol(sFolder, col) = 0 Then
MsgBox "No xls in " & sFolder
Exit Sub
End If

Application.EnableEvents = False
Application.ScreenUpdating = False
ReDim va(1 To col.Count, 1 To 2)

For i = 1 To col.Count

'first test if the file is already open
On Error Resume Next
Set wb = Workbooks(col(i))
On Error GoTo errH

If wb Is Nothing Then
' not open so open it and flag to close later
Set wb = Workbooks.Open(sFolder & "\" & col(i))
bWasClosed = True
End If

' process your files here or pass wb to another procedure
' eg
va(i, 1) = col(i)
va(i, 2) = wb.Worksheets(1).Range("A1").Value

If Not wb.Saved Then
wb.Save
End If

wb.Close
Set wb = Nothing
bWasClosed = False
Next

With Workbooks.Add
.Worksheets(1).Range("A1:B1").Resize(UBound(va)).V alue = va
End With

errH:
Application.EnableEvents = True
Application.ScreenUpdating = True

If Err.Number Then MsgBox "Error"

End Sub

This populates an array of file names & values in A1 of the first worksheet,
finally dumps the array into a new workbook.

In my Temp folder I had files with open events, hence I disabled
enableevents in this demo, you might not want to do that.

Regards,
Peter T

"Ben" wrote in message
...
That's great but how would I sequentially open the files then perform the
code then close the file

"Ben" wrote:



"AA2e72E" wrote:

This code will give you a starting point:

Sub aa()
Set fso =
CreateObject("Scripting.FileSystemObject").GetFold er("c:\OurFiles\")
For Each file In fso.Files
If file.Type = "Microsoft Excel Worksheet" Then
With file
' do what you want with this file
End With
End If
Next
Set fso = Nothing
End Sub

Note: Replace "C:\OURFILES" by your folder; this code does not recurse

the
foldre tree if it has one.

"Ben" wrote:

I would like to perform a piece of code on every workbook in a

folder and
then close it. What would be the best way please. Thank you





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Cycling through all worbooks in a folder

I intended to close the wb only if it was not previously open, so change -

wb.Close


to -

If bWasClosed Then wb.Close

Peter T


"Peter T" <peter_t@discussions wrote in message
...
Hi Ben,

Maybe you can adapt this to your needs -

Option Explicit

Function FilesToCol(sPath As String, c As Collection) As Long
Dim sFile As String

sFile = Dir(sPath & "\*.xls")
Do While Len(sFile)
c.Add sFile
sFile = Dir()
Loop
FilesToCol = c.Count

End Function

Sub Test()
Dim bWasClosed As Boolean
Dim sFolder As String
Dim col As Collection
Dim i As Long
Dim wb As Workbook

Set col = New Collection

sFolder = "C:\Temp"

If FilesToCol(sFolder, col) = 0 Then
MsgBox "No xls in " & sFolder
Exit Sub
End If

Application.EnableEvents = False
Application.ScreenUpdating = False
ReDim va(1 To col.Count, 1 To 2)

For i = 1 To col.Count

'first test if the file is already open
On Error Resume Next
Set wb = Workbooks(col(i))
On Error GoTo errH

If wb Is Nothing Then
' not open so open it and flag to close later
Set wb = Workbooks.Open(sFolder & "\" & col(i))
bWasClosed = True
End If

' process your files here or pass wb to another procedure
' eg
va(i, 1) = col(i)
va(i, 2) = wb.Worksheets(1).Range("A1").Value

If Not wb.Saved Then
wb.Save
End If

wb.Close
Set wb = Nothing
bWasClosed = False
Next

With Workbooks.Add
.Worksheets(1).Range("A1:B1").Resize(UBound(va)).V alue = va
End With

errH:
Application.EnableEvents = True
Application.ScreenUpdating = True

If Err.Number Then MsgBox "Error"

End Sub

This populates an array of file names & values in A1 of the first

worksheet,
finally dumps the array into a new workbook.

In my Temp folder I had files with open events, hence I disabled
enableevents in this demo, you might not want to do that.

Regards,
Peter T

"Ben" wrote in message
...
That's great but how would I sequentially open the files then perform

the
code then close the file

"Ben" wrote:



"AA2e72E" wrote:

This code will give you a starting point:

Sub aa()
Set fso =
CreateObject("Scripting.FileSystemObject").GetFold er("c:\OurFiles\")
For Each file In fso.Files
If file.Type = "Microsoft Excel Worksheet" Then
With file
' do what you want with this file
End With
End If
Next
Set fso = Nothing
End Sub

Note: Replace "C:\OURFILES" by your folder; this code does not

recurse
the
foldre tree if it has one.

"Ben" wrote:

I would like to perform a piece of code on every workbook in a

folder and
then close it. What would be the best way please. Thank you





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
Tab keeps cycling through the same row Mike Excel Discussion (Misc queries) 2 August 14th 08 12:20 AM
Opening worbooks with a variable file name. ibbm Excel Programming 5 January 5th 06 02:58 AM
Clarification on "Application.Worbooks.Count" proceedure Les Stout[_2_] Excel Programming 8 November 2nd 05 07:12 PM
cycling through columns? Kirk Excel Programming 2 May 2nd 05 07:51 PM
Cycling through variables. Azza Excel Programming 1 October 14th 04 10:23 PM


All times are GMT +1. The time now is 10:36 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"