PDA

View Full Version : How to add a row in different files?



Luiz
06-03-2008, 06:15 AM
Hello,

How are you?
I?m needing help of you...
I have many files in one folder and it is distributed in sub folders. And I have to add a specific row in each file (obviously each file has a different name..).
Note: The row to be added is the same for all files.
Is there a way to do this? Is there a macro for this?

Thanks a lot!!!

mdmackillop
06-03-2008, 07:03 AM
Based on this KB Item (http://www.vbaexpress.com/kb/getarticle.php?kb_id=245)
This will add a row to Sheet1 Range A5 in all workboks in a folder and its sub folders


Option Explicit
Option Compare Text

Sub OneType()
Const MyPath = "C:\CSV" ' Set the path.
Const FileType = "*.xls" ' or "*.doc"
ProcessFiles MyPath, FileType
End Sub


Sub ProcessFiles(strFolder As String, strFilePattern As String)
Dim strFileName As String
Dim strFolders() As String
Dim iFolderCount As Integer
Dim i As Integer
Dim wb As Workbook

'Collect child folders
strFileName = Dir$(strFolder & "\", vbDirectory)
Do Until strFileName = ""
If (GetAttr(strFolder & "\" & strFileName) And vbDirectory) = vbDirectory Then
If Left$(strFileName, 1) <> "." Then
ReDim Preserve strFolders(iFolderCount)
strFolders(iFolderCount) = strFolder & "\" & strFileName
iFolderCount = iFolderCount + 1
End If
End If
strFileName = Dir$()
Loop

'process files in current folder
strFileName = Dir$(strFolder & "\" & strFilePattern)
Do Until strFileName = ""
'Do things with files here*****************
Debug.Print strFolder & "\" & strFileName
Set wb = Workbooks.Open(strFolder & "\" & strFileName)
wb.Sheets(1).Range("A5").EntireRow.Insert
wb.Close True

'*******************************************
strFileName = Dir$()
Loop

'Look through child folders
For i = 0 To iFolderCount - 1
ProcessFiles strFolders(i), strFilePattern
Next i
End Sub