Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 31
Default run macro for all files in the directory

Hi,

I am trying to figure out how to run a macro for all the
files in the directory without specifying each individual
file.
I want the macro to open 1st file, run macro, close the
first file; then open the second file and do the same
thing for all other files.

Please help with the code...

Thank you
Igor
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default run macro for all files in the directory

Sub WorkWithFiles()
Dim as long
Dim wkbk as Workbook
With Application.FileSearch
.NewSearch
.LookIn = "C:\MyFolder"
.SearchSubFolders = False
.FileName = ".xls"
' .FileType = msoFileTypeAllFiles
.FileType = msoFileTypeExcelWorkbooks
If .Execute() 0 Then
For i = 1 To .FoundFiles.Count
set wkbk = Workbooks.Open(.Foundfiles(i))
' work with the wkbk reference
' macro1
wkbk.Close SaveChanges:=False
Else
MsgBox "There were no files found."
End If
End With
End Sub

Regards,
Tom Ogilvy

"igor" wrote in message
...
Hi,

I am trying to figure out how to run a macro for all the
files in the directory without specifying each individual
file.
I want the macro to open 1st file, run macro, close the
first file; then open the second file and do the same
thing for all other files.

Please help with the code...

Thank you
Igor



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 31
Default run macro for all files in the directory

thank you veeeeeeeeery much
-----Original Message-----
Sub WorkWithFiles()
Dim as long
Dim wkbk as Workbook
With Application.FileSearch
.NewSearch
.LookIn = "C:\MyFolder"
.SearchSubFolders = False
.FileName = ".xls"
' .FileType = msoFileTypeAllFiles
.FileType = msoFileTypeExcelWorkbooks
If .Execute() 0 Then
For i = 1 To .FoundFiles.Count
set wkbk = Workbooks.Open(.Foundfiles(i))
' work with the wkbk reference
' macro1
wkbk.Close SaveChanges:=False
Else
MsgBox "There were no files found."
End If
End With
End Sub

Regards,
Tom Ogilvy

"igor" wrote in message
...
Hi,

I am trying to figure out how to run a macro for all the
files in the directory without specifying each

individual
file.
I want the macro to open 1st file, run macro, close the
first file; then open the second file and do the same
thing for all other files.

Please help with the code...

Thank you
Igor



.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 68
Default run macro for all files in the directory

Option Explicit


Public Sub ReadExcelFiles(FolderName As String)
Dim FileName As String

' Add trailing \ character if necessary
'
If Right(FolderName, 1) < "\" Then FolderName = FolderName & "\"

FileName = Dir(FolderName & "*.xls")

Do While FileName < ""
Workbooks.Open (FolderName & FileName)

' Do whatever workbook manipulation here

Workbooks(FileName).Close
FileName = Dir()
Loop
End Sub

Public Sub test()
ReadExcelFiles ("c:\temp\test")
End Sub

http://www.billlunney.com/Excel/FAQ/...ExcelFAQID=204


--

Regards,


Bill Lunney
www.billlunney.com

"igor" wrote in message
...
Hi,

I am trying to figure out how to run a macro for all the
files in the directory without specifying each individual
file.
I want the macro to open 1st file, run macro, close the
first file; then open the second file and do the same
thing for all other files.

Please help with the code...

Thank you
Igor



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
open all files in directory davethewelder Excel Discussion (Misc queries) 2 August 26th 08 02:01 PM
Macro - Open all word files in a directory Dileep Chandran Excel Worksheet Functions 11 December 19th 06 02:12 PM
List of Files in A Directory JaneC Excel Discussion (Misc queries) 2 February 18th 06 12:11 PM
Files in a directory? Greg B Excel Discussion (Misc queries) 5 May 11th 05 09:46 PM
Load all files in a directory Greg B... Excel Discussion (Misc queries) 2 March 2nd 05 12:22 PM


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