Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 24
Default for-each-next loop

Dear experts,
I would like to open through a macro each of the workbooks
that are in a certain folder. (Their number and names can
vary over time).
I wanted to use the FOR EACH... IN... NEXT loop, but it
does not seem to work (I get a "Type mismatch" error
message). Do you know how I could perform this?
Many thanks in advance.
Regards,
Valeria
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default for-each-next loop

Valeria,

This works in XL2000, should also work in 2003 and 97

Sub OpenFiles(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)

"Valeria" wrote in message
...
Dear experts,
I would like to open through a macro each of the workbooks
that are in a certain folder. (Their number and names can
vary over time).
I wanted to use the FOR EACH... IN... NEXT loop, but it
does not seem to work (I get a "Type mismatch" error
message). Do you know how I could perform this?
Many thanks in advance.
Regards,
Valeria



  #3   Report Post  
Posted to microsoft.public.excel.programming
No Name
 
Posts: n/a
Default for-each-next loop

Hi Bob,
it works perfectly!
Thanks a lot,
Valeria

-----Original Message-----
Valeria,

This works in XL2000, should also work in 2003 and 97

Sub OpenFiles(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)

"Valeria" wrote in

message
...
Dear experts,
I would like to open through a macro each of the

workbooks
that are in a certain folder. (Their number and names

can
vary over time).
I wanted to use the FOR EACH... IN... NEXT loop, but it
does not seem to work (I get a "Type mismatch" error
message). Do you know how I could perform this?
Many thanks in advance.
Regards,
Valeria



.

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
for/next loop Poor Richard New Users to Excel 3 August 31st 09 09:28 PM
For..Next..Loop default105 Excel Discussion (Misc queries) 12 July 7th 09 07:46 PM
Find loop doesn't loop JSnow Excel Discussion (Misc queries) 2 June 24th 09 08:28 PM
Loop help PaulW Excel Discussion (Misc queries) 0 May 8th 06 04:00 PM
getting out of a if loop rk0909 Excel Discussion (Misc queries) 3 April 11th 06 03:55 PM


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