ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   a macro to run on all the file in the same folder (https://www.excelbanter.com/excel-programming/349471-macro-run-all-file-same-folder.html)

ditzafar[_9_]

a macro to run on all the file in the same folder
 

I want to write a macro that will run on all the file in the sam
folder.
how do I do it?
thanks:

--
ditzafa
-----------------------------------------------------------------------
ditzafar's Profile: http://www.excelforum.com/member.php...fo&userid=2947
View this thread: http://www.excelforum.com/showthread.php?threadid=49790


Bob Phillips[_6_]

a macro to run on all the file in the same folder
 
Here's some code to open all files in a folder. just call your macro after
opening

im aryFiles
Dim oFSO

Sub LoopFolders()
Dim i As Integer

Set oFSO = CreateObject("Scripting.FileSystemObject")

selectFiles "c:\MyTest"

Set oFSO = Nothing

End Sub


'---------------------------------------------------------------------------
Sub selectFiles(sPath)
'---------------------------------------------------------------------------
Dim Folder As Object
Dim Files As Object
Dim file As Object
Dim fldr

Set Folder = oFSO.GetFolder(sPath)

For Each fldr In Folder.Subfolders
selectFiles fldr.Path
Next fldr

For Each file In Folder.Files
If file.Type = "Microsoft Excel Worksheet" Then
Workbooks.Open Filename:=file.Path
' your macro here
Activeworkbook.Save
Activeworkbook.Close
End If
Next file

End Sub


--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"ditzafar" wrote in
message ...

I want to write a macro that will run on all the file in the same
folder.
how do I do it?
thanks:)


--
ditzafar
------------------------------------------------------------------------
ditzafar's Profile:

http://www.excelforum.com/member.php...o&userid=29475
View this thread: http://www.excelforum.com/showthread...hreadid=497905




davidm

a macro to run on all the file in the same folder
 

Bob,

Your code is great! I have been looking for something like that for
quite some time now. Good to hear Ditzafar is not the only beneficiary
:)


David


--
davidm
------------------------------------------------------------------------
davidm's Profile: http://www.excelforum.com/member.php...o&userid=20645
View this thread: http://www.excelforum.com/showthread...hreadid=497905


ditzafar[_10_]

a macro to run on all the file in the same folder
 

I have same questions.
1. im aryFiles did you mean dim aryfiles. if you mean it, for what do
need it?
2. for what do I need Dim i As Integer
3. selectFiles "c:\MyTest" did it is the name of the folder
if it the name of the folder where does this macro run on all th
files
4.when I runing this macro I get a erro message "sub or function no
define".
for the function selectFiles
5. Should I call my macro between this two line
selectFiles "c:\MyTest"

thanks

Set oFSO = Nothing

Bob Phillips Wrote:
Here's some code to open all files in a folder. just call your macr
after
opening

im aryFiles
Dim oFSO

Sub LoopFolders()
Dim i As Integer

Set oFSO = CreateObject("Scripting.FileSystemObject")

selectFiles "c:\MyTest"

Set oFSO = Nothing

End Sub


'---------------------------------------------------------------------------
Sub selectFiles(sPath)
'---------------------------------------------------------------------------
Dim Folder As Object
Dim Files As Object
Dim file As Object
Dim fldr

Set Folder = oFSO.GetFolder(sPath)

For Each fldr In Folder.Subfolders
selectFiles fldr.Path
Next fldr

For Each file In Folder.Files
If file.Type = "Microsoft Excel Worksheet" Then
Workbooks.Open Filename:=file.Path
' your macro here
Activeworkbook.Save
Activeworkbook.Close
End If
Next file

End Sub


--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"ditzafar"
wrote in
message ...

I want to write a macro that will run on all the file in the same
folder.
how do I do it?
thanks:)


--
ditzafar


------------------------------------------------------------------------
ditzafar's Profile:

http://www.excelforum.com/member.php...o&userid=29475
View this thread

http://www.excelforum.com/showthread...hreadid=497905


--
ditzafa
-----------------------------------------------------------------------
ditzafar's Profile: http://www.excelforum.com/member.php...fo&userid=2947
View this thread: http://www.excelforum.com/showthread.php?threadid=49790


Bob Phillips[_6_]

a macro to run on all the file in the same folder
 
A 2 for one'er :-))

Bob

"davidm" wrote in
message ...

Bob,

Your code is great! I have been looking for something like that for
quite some time now. Good to hear Ditzafar is not the only beneficiary
:)


David


--
davidm
------------------------------------------------------------------------
davidm's Profile:

http://www.excelforum.com/member.php...o&userid=20645
View this thread: http://www.excelforum.com/showthread...hreadid=497905




Bob Phillips[_6_]

a macro to run on all the file in the same folder
 

"ditzafar" wrote in
message ...

I have same questions.
1. im aryFiles did you mean dim aryfiles. if you mean it, for what do I
need it?


I do, the copy got chopped.

2. for what do I need Dim i As Integer


You don't need either, I cut this from some other code, and left some
redundant stuff in.

3. selectFiles "c:\MyTest" did it is the name of the folder
if it the name of the folder where does this macro run on all the
files


Yes

4.when I runing this macro I get a erro message "sub or function not
define".
for the function selectFiles


I don't, I am not sure why you should. As you can see the sub exists.

5. Should I call my macro between this two line


I have marked where you should call your macro.



ditzafar[_11_]

a macro to run on all the file in the same folder
 

thanks
your macro save me a lot of time.

Bob Phillips Wrote:
"ditzafar" wrote
in
message ...

I have same questions.
1. im aryFiles did you mean dim aryfiles. if you mean it, for what do

I
need it?


I do, the copy got chopped.

2. for what do I need Dim i As Integer


You don't need either, I cut this from some other code, and left some
redundant stuff in.

3. selectFiles "c:\MyTest" did it is the name of the folder
if it the name of the folder where does this macro run on all the
files


Yes

4.when I runing this macro I get a erro message "sub or function not
define".
for the function selectFiles


I don't, I am not sure why you should. As you can see the sub exists.

5. Should I call my macro between this two line


I have marked where you should call your macro.



--
ditzafar
------------------------------------------------------------------------
ditzafar's Profile: http://www.excelforum.com/member.php...o&userid=29475
View this thread: http://www.excelforum.com/showthread...hreadid=497905



All times are GMT +1. The time now is 07:18 PM.

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