"Do you want this to run against all workbooks found in those directories?:
Yes
o/p would be
Id Name Age CName
1 ABC 12 CompanyName
2 XYZ 43 CompanyName
3 YUI 41 CompanyName
Main Topics
Browse All TopicsI have a directory structure like
Main Directory
Sub Directory A
Sub Directory B
Sub Directory C
The sub directories contains more than one excel files.
Each excel file has got two worksheets ( Detail Sheet, Summary Sheet )
The format of the excel worksheets are identical.
The first row of Detail Sheet COntains the header part , say
Id, Name,Address,Age,Descripti
The third row of Cell C in the summary sheet contains the company's name
What we are looking for is a macro which will traverse thru the subdirectoires and
copy into a single excel file the information of CellA,CellB,CellD of Detail Sheet and
the Company name from Summary Sheet.Alos the new worksheet should have only one header.
So for ex assuming an excel workbook format of
Detail Sheet
A B C D E
Id Name Address Age Description
1 ABC YTE 12 Long
2 XYZ REW 43 Short
3 YUI DD 41 Short
4 RET FDF 12 Long
5 BBB FDF 54 Medium
Summary Sheet
A B C
Info Info Info
Info Info Info
Info Info RequiredValue
How can we go about it.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
OK, then try this:
Sub ListSubFiles(strParentFold
Dim fs As FileSearch
Dim lngCounter As Long, lngRow As Long, lngOutputRow As Long
Dim wbk As Workbook, wks1 As Worksheet, wks2 As Worksheet, wksSummary As Worksheet
Dim rngData As Range, rngCell As Range
Dim strCompany As String
Application.ScreenUpdating
Set wksSummary = ActiveWorkbook.ActiveSheet
lngOutputRow = 1
Set fs = Application.FileSearch
With fs
.NewSearch
.LookIn = strParentFolder
.SearchSubFolders = True
.FileName = "*.xls"
.MatchTextExactly = True
.FileType = msoFileTypeExcelWorkbooks
.Execute
' Loop through all the found files
For lngCounter = 1 To .FoundFiles.Count
Set wbk = Workbooks.Open(.FoundFiles
Set wks1 = wbk.Sheets("Detail sheet")
Set wks2 = wbk.Sheets("Summary sheet")
strCompany = wks2.Range("C3")
' Assumes there will always be data in column 1
Set rngData = Intersect(wks1.UsedRange, wks1.Columns(1))
For Each rngCell In rngData
' Write columns 1, 2 and 4 and company name to sheet 1 of this workbook
wksSummary.Cells(lngOutput
wksSummary.Cells(lngOutput
wksSummary.Cells(lngOutput
wksSummary.Cells(lngOutput
lngOutputRow = lngOutputRow + 1
Exit For
Next rngCell
wbk.Close False
Next lngCounter
Set rngData = Nothing
Set wks1 = Nothing
Set wks2 = Nothing
Set wbk = Nothing
End With
Set fs = Nothing
Application.ScreenUpdating
End Sub
called by ListSubFiles ParentFolderNameAndPath
HTH
Rory
You would copy it into a module in either a new workbook or in your Personal.xls file lif you have one. Then run it with a macro that looks like:
Sub DoList()
ListSubFiles "C:\Parent Folder"
End Sub.
I have just noticed a mistake in the code though - it should be:
Sub ListSubFiles(strParentFold
Dim fs As FileSearch
Dim lngCounter As Long, lngRow As Long, lngOutputRow As Long
Dim wbk As Workbook, wks1 As Worksheet, wks2 As Worksheet, wksSummary As Worksheet
Dim rngData As Range, rngCell As Range
Dim strCompany As String
Application.ScreenUpdating
Set wksSummary = ActiveWorkbook.ActiveSheet
lngOutputRow = 1
Set fs = Application.FileSearch
With fs
.NewSearch
.LookIn = strParentFolder
.SearchSubFolders = True
.FileName = "*.xls"
.MatchTextExactly = True
.FileType = msoFileTypeExcelWorkbooks
.Execute
' Loop through all the found files
For lngCounter = 1 To .FoundFiles.Count
Set wbk = Workbooks.Open(.FoundFiles
Set wks1 = wbk.Sheets("Detail sheet")
Set wks2 = wbk.Sheets("Summary sheet")
strCompany = wks2.Range("C3")
' Assumes there will always be data in column 1
Set rngData = Intersect(wks1.UsedRange, wks1.Columns(1))
For Each rngCell In rngData
' Write columns 1, 2 and 4 and company name to sheet 1 of this workbook
wksSummary.Cells(lngOutput
wksSummary.Cells(lngOutput
wksSummary.Cells(lngOutput
wksSummary.Cells(lngOutput
lngOutputRow = lngOutputRow + 1
Next rngCell
wbk.Close False
Next lngCounter
Set rngData = Nothing
Set wks1 = Nothing
Set wks2 = Nothing
Set wbk = Nothing
End With
Set fs = Nothing
Application.ScreenUpdating
End Sub
If that produces an error, can you tell me what the error is?
Regards,
Rory
guess ur above code removed that error, but i dont want the headers to be repeated each time.
the header should come only once. and the info of CELL C row 3 gets written into the header also
now the o/p comes like
Id Name Age The Companyname
1 ABC 12 CompanyName
Id Name Age The Companyname2
2 VCX 54 The Companyname2
while i want the o/p to be like
Id Name Age CName
1 ABC 12 CompanyName
2 XYZ 43 CompanyName
3 YUI 41 CompanyName
4 CV 12 SecondCompany
5 AE 56 SecondCompany
6 RD 53 SecondCompany
7 XC 56 SecondCompany
Try this version:
Sub ListSubFiles(strParentFold
Dim fs As FileSearch
Dim lngCounter As Long, lngRow As Long, lngOutputRow As Long
Dim wbk As Workbook, wks1 As Worksheet, wks2 As Worksheet, wksSummary As Worksheet
Dim rngData As Range, rngCell As Range
Dim strCompany As String
Application.ScreenUpdating
Set wksSummary = ActiveWorkbook.ActiveSheet
lngOutputRow = 1
Set fs = Application.FileSearch
With fs
.NewSearch
.LookIn = strParentFolder
.SearchSubFolders = True
.FileName = "*.xls"
.MatchTextExactly = True
.FileType = msoFileTypeExcelWorkbooks
.Execute
' Loop through all the found files
For lngCounter = 1 To .FoundFiles.Count
Set wbk = Workbooks.Open(.FoundFiles
Set wks1 = wbk.Sheets("Detail sheet")
Set wks2 = wbk.Sheets("Summary sheet")
strCompany = wks2.Range("C3")
' Assumes there will always be data in column 1
If lngCounter = 1 Then
Set rngData = Intersect(wks1.UsedRange, wks1.Columns(1))
Else
Set rngData = Intersect(wks1.UsedRange, wks1.Range("A2:A65536"))
End If
For Each rngCell In rngData
' Write columns 1, 2 and 4 and company name to sheet 1 of this workbook
wksSummary.Cells(lngOutput
wksSummary.Cells(lngOutput
wksSummary.Cells(lngOutput
If lngOutputRow > 1 Then wksSummary.Cells(lngOutput
lngOutputRow = lngOutputRow + 1
Next rngCell
wbk.Close False
Next lngCounter
Set rngData = Nothing
Set wks1 = Nothing
Set wks2 = Nothing
Set wbk = Nothing
End With
Set fs = Nothing
Application.ScreenUpdating
End Sub
HTH
Rory
Business Accounts
Answer for Membership
by: roryaPosted on 2006-08-14 at 04:30:59ID: 17309262
Hi,
Do you want this to run against all workbooks found in those directories? Also, am I correct in thinking that your output should look like this, from your example:
1 ABC 12 CompanyName
2 XYZ 43 CompanyName
3 YUI 41 CompanyName
If not, how should it look?
Rory