Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 217
Default Applying a macro to all Excel-files in a folder

How do I apply a macro to all excel sheets in a folder? (in my case I need to
insert a row with certain content in row 16 in 700 excel sheets). All sheets
are alike.

Thank you!
Louise
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Applying a macro to all Excel-files in a folder

One way

Sub ProcessFiles()
Dim i As Long
Dim sFolder As String
Dim fldr As Object
Dim FSO As Object
Dim Folder As Object
Dim file As Object
Dim Files As Object
Dim this As Workbook
Dim cnt As Long

Set FSO = CreateObject("Scripting.FileSystemObject")

Set this = ActiveWorkbook
sFolder = "C:\MyTest"
If sFolder < "" Then
Set Folder = FSO.GetFolder(sFolder)

Set Files = Folder.Files
cnt = 1
For Each file In Files
If file.Type = "Microsoft Excel Worksheet" Then
Workbooks.Open Filename:=file.Path
With ActiveWorkbook
With .Worksheets(1)
.Range("A16").EntireRow.Insert
.Range("A16").Value = "ABC"
'etc.
End With
.Save
.Close
End With
cnt = cnt + 1
End If
Next file

End If ' sFolder < ""

End Sub



--

HTH

RP
(remove nothere from the email address if mailing direct)


"Louise" wrote in message
...
How do I apply a macro to all excel sheets in a folder? (in my case I need

to
insert a row with certain content in row 16 in 700 excel sheets). All

sheets
are alike.

Thank you!
Louise



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 217
Default Applying a macro to all Excel-files in a folder

Hi Bob thank you for your fast reply!

Unfortunately I cant make it work. I pasted your code into VB in a new Excel
workbook, put in the correct path (sFolder = "C:\MyTest") and the correct
values for row 16 (.Range("A16").Value = "ABC"). however nothing happend.
Please excuse with me, I am using this for the first time :l

Where do I put the code to run it and how do I specify that it is sheet no.
2 that this needs done to?

Many many thanks
Louise

"Bob Phillips" wrote:

One way

Sub ProcessFiles()
Dim i As Long
Dim sFolder As String
Dim fldr As Object
Dim FSO As Object
Dim Folder As Object
Dim file As Object
Dim Files As Object
Dim this As Workbook
Dim cnt As Long

Set FSO = CreateObject("Scripting.FileSystemObject")

Set this = ActiveWorkbook
sFolder = "C:\MyTest"
If sFolder < "" Then
Set Folder = FSO.GetFolder(sFolder)

Set Files = Folder.Files
cnt = 1
For Each file In Files
If file.Type = "Microsoft Excel Worksheet" Then
Workbooks.Open Filename:=file.Path
With ActiveWorkbook
With .Worksheets(1)
.Range("A16").EntireRow.Insert
.Range("A16").Value = "ABC"
'etc.
End With
.Save
.Close
End With
cnt = cnt + 1
End If
Next file

End If ' sFolder < ""

End Sub



--

HTH

RP
(remove nothere from the email address if mailing direct)


"Louise" wrote in message
...
How do I apply a macro to all excel sheets in a folder? (in my case I need

to
insert a row with certain content in row 16 in 700 excel sheets). All

sheets
are alike.

Thank you!
Louise




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Applying a macro to all Excel-files in a folder

Sub AddRow
Dim sPath as String, sName as String
Dim v as Variant
Dim i as Long, bk as Workbook
redim v(1 to 1000)

i = 0
sPath = "C:\MyFolder\"
sName = dir(sPath & "*.xls")
do while sName < ""
i = i + 1
v(i) = sName
sName = Dir()
Loop
redim preserve v(1 to i)
for i = lbound(v) to ubound(v)
set bk = workbooks.Open(sPath & v(i))
With bk.worksheets(1)
.Rows(16).Insert
.Cells(16,1).Value = "ABCE"
' whatever else
end with
bk.Close Savechanges:=True
Next

End Sub

code is untested. You should get it working and test it on a dummy
directory with a few files in it.
--
Regards,
Tom Ogilvy


"Louise" wrote in message
...
How do I apply a macro to all excel sheets in a folder? (in my case I need

to
insert a row with certain content in row 16 in 700 excel sheets). All

sheets
are alike.

Thank you!
Louise



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Applying a macro to all Excel-files in a folder

Hi Louise,

I just tested it again, pasting it in from the posting, and it worked fine
for me. Make sure that you add the code to a standard code module.

Make sure that no extraneous - get inserted, this seems to be a relatively
new feature. If you look at the code in VBE, check there is no red text.

As for sheet2, change Worksheets(1) to Worksheets(2)

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Louise" wrote in message
...
Hi Bob thank you for your fast reply!

Unfortunately I cant make it work. I pasted your code into VB in a new

Excel
workbook, put in the correct path (sFolder = "C:\MyTest") and the correct
values for row 16 (.Range("A16").Value = "ABC"). however nothing happend.
Please excuse with me, I am using this for the first time :l

Where do I put the code to run it and how do I specify that it is sheet

no.
2 that this needs done to?

Many many thanks
Louise

"Bob Phillips" wrote:

One way

Sub ProcessFiles()
Dim i As Long
Dim sFolder As String
Dim fldr As Object
Dim FSO As Object
Dim Folder As Object
Dim file As Object
Dim Files As Object
Dim this As Workbook
Dim cnt As Long

Set FSO = CreateObject("Scripting.FileSystemObject")

Set this = ActiveWorkbook
sFolder = "C:\MyTest"
If sFolder < "" Then
Set Folder = FSO.GetFolder(sFolder)

Set Files = Folder.Files
cnt = 1
For Each file In Files
If file.Type = "Microsoft Excel Worksheet" Then
Workbooks.Open Filename:=file.Path
With ActiveWorkbook
With .Worksheets(1)
.Range("A16").EntireRow.Insert
.Range("A16").Value = "ABC"
'etc.
End With
.Save
.Close
End With
cnt = cnt + 1
End If
Next file

End If ' sFolder < ""

End Sub



--

HTH

RP
(remove nothere from the email address if mailing direct)


"Louise" wrote in message
...
How do I apply a macro to all excel sheets in a folder? (in my case I

need
to
insert a row with certain content in row 16 in 700 excel sheets). All

sheets
are alike.

Thank you!
Louise








  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 217
Default Applying a macro to all Excel-files in a folder

Hi Tom,
where do I put this code? In a module in a new Excel sheet? Can I run it
from there?
I have tried and it doesn't seem to work?
Thanks Louise

"Tom Ogilvy" wrote:

Sub AddRow
Dim sPath as String, sName as String
Dim v as Variant
Dim i as Long, bk as Workbook
redim v(1 to 1000)

i = 0
sPath = "C:\MyFolder\"
sName = dir(sPath & "*.xls")
do while sName < ""
i = i + 1
v(i) = sName
sName = Dir()
Loop
redim preserve v(1 to i)
for i = lbound(v) to ubound(v)
set bk = workbooks.Open(sPath & v(i))
With bk.worksheets(1)
.Rows(16).Insert
.Cells(16,1).Value = "ABCE"
' whatever else
end with
bk.Close Savechanges:=True
Next

End Sub

code is untested. You should get it working and test it on a dummy
directory with a few files in it.
--
Regards,
Tom Ogilvy


"Louise" wrote in message
...
How do I apply a macro to all excel sheets in a folder? (in my case I need

to
insert a row with certain content in row 16 in 700 excel sheets). All

sheets
are alike.

Thank you!
Louise




  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Applying a macro to all Excel-files in a folder

You would put it in the general module of a workbook that you will use to
run the code from. (not one of the workbooks being changed or a workbook in
the directory you want to process).

I have tried and it doesn't seem to work?

I copied the code out of your email and pointed it at a directory with 3
workbooks. I then ran the macro. It opened each, went to the first sheet,
inserted a new row 16 and in column A, entered ABCDE

So, it does what I designed it to do. Obviously beyond inserting the new
row 16 on the first sheet in the tab order (which sheet was an assumption
since you didn't say), you would have to modify it to meet your specific
requirements. Enjoy!

--
Regards,
Tom Ogilvy




"Louise" wrote in message
...
Hi Tom,
where do I put this code? In a module in a new Excel sheet? Can I run it
from there?
I have tried and it doesn't seem to work?
Thanks Louise

"Tom Ogilvy" wrote:

Sub AddRow
Dim sPath as String, sName as String
Dim v as Variant
Dim i as Long, bk as Workbook
redim v(1 to 1000)

i = 0
sPath = "C:\MyFolder\"
sName = dir(sPath & "*.xls")
do while sName < ""
i = i + 1
v(i) = sName
sName = Dir()
Loop
redim preserve v(1 to i)
for i = lbound(v) to ubound(v)
set bk = workbooks.Open(sPath & v(i))
With bk.worksheets(1)
.Rows(16).Insert
.Cells(16,1).Value = "ABCE"
' whatever else
end with
bk.Close Savechanges:=True
Next

End Sub

code is untested. You should get it working and test it on a dummy
directory with a few files in it.
--
Regards,
Tom Ogilvy


"Louise" wrote in message
...
How do I apply a macro to all excel sheets in a folder? (in my case I

need
to
insert a row with certain content in row 16 in 700 excel sheets). All

sheets
are alike.

Thank you!
Louise






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
Macro to create a folder and copy files GainesvilleWes New Users to Excel 2 February 26th 07 06:33 PM
how can I specific a folder with wildcard criteria and excel will import all the correct files in that folder? Raven Excel Discussion (Misc queries) 1 January 24th 06 03:28 PM
Macro to copy range from Excel files in folder nc Excel Discussion (Misc queries) 1 June 15th 05 11:11 AM
Help - applying macro to all files in folder jrc123 Excel Programming 1 November 5th 04 07:10 PM
Macro to Print All Files in a Folder Jim May Excel Programming 5 July 18th 04 04:09 PM


All times are GMT +1. The time now is 10:38 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"