Hi Bud,
Though Patrick has what I think you're looking for, you could also use something like this to combine all the data into as few sheets as necessary. Of course this assumes your data all has the same format, but will work regardless:
Option Explicit
Sub CombineSheetsFromAllFilesI
Dim Path As String 'string variable to hold the path to look through
Dim FileName As String 'temporary filename string variable
Dim tWB As Workbook 'temporary workbook (each in directory)
Dim tWS As Worksheet 'temporary worksheet variable
Dim mWB As Workbook 'master workbook
Dim aWS As Worksheet 'active sheet in master workbook
Dim RowCount As Long 'Rows used on master sheet
Dim uRange As Range 'usedrange for each temporary sheet
Dim LastColm As Range 'Range variable, will be used to find the last used column
'***** Set folder to cycle through *****
Path = "C:\temp\" 'Change as needed
Application.EnableEvents = False 'turn off events
Application.ScreenUpdating
Set mWB = Workbooks.Add(1) 'create a new one-worksheet workbook
Set aWS = mWB.ActiveSheet 'set active sheet variable to only sheet in mWB
If Right(Path, 1) <> Application.PathSeparator Then 'if path doesnt end in "\"
Path = Path & Application.PathSeparator 'add "\"
End If
FileName = Dir(Path & "*.xls", vbNormal) 'set first file's name to filename variable
Do Until FileName = "" 'loop until all files have been parsed
If Path <> ThisWorkbook.Path Or FileName <> ThisWorkbook.Name Then
Set tWB = Workbooks.Open(FileName:=P
For Each tWS In tWB.Worksheets 'loop through each sheet
Set uRange = tWS.Range("A2", tWS.Cells(tWS.UsedRange.Ro
.Count - 1, tWS.UsedRange.Column + tWS.UsedRange.Columns.Coun
If RowCount + uRange.Rows.Count > 65536 Then 'if the used range wont fit on the sheet
aWS.Columns.AutoFit 'autofit mostly-used worksheet's columns
Set LastColm = aWS.Cells.Find(What:="*", After:=Range("IV1"), _
SearchOrder:=xlByColumns, SearchDirection:=xlPreviou
If LastColm.Column <> 255 Then
aWS.Range(aWS.Columns(Last
End If
RowCount = aWS.UsedRange.Rows.Count 'Reset scroll bars and last cell
Set aWS = mWB.Sheets.Add(After:=aWS)
RowCount = 0 'reset RowCount variable
End If
If RowCount = 0 Then 'if working with a new sheet
aWS.Range("A1", aWS.Cells(1, uRange.Columns.Count)).Val
tWS.Range("A1", tWS.Cells(1, uRange.Columns.Count)).Val
aWS.Range("IV1").Value = "Source Sheet"
RowCount = 1 'add one to rowcount
End If
With aWS.Range("A" & RowCount + 1).Resize(uRange.Rows.Coun
.Value = uRange.Value 'move data from temp sheet to data sheet
Intersect(.EntireRow, aWS.Columns("IV")).Value = tWS.Name
End With
RowCount = RowCount + uRange.Rows.Count 'increase rowcount accordingly
Next 'tWS
tWB.Close False 'close temporary workbook without saving
End If
FileName = Dir() 'set next file's name to FileName variable
Loop
aWS.Columns.AutoFit 'autofit columns on last data sheet
Set LastColm = aWS.Cells.Find(What:="*", After:=Range("IV1"), _
SearchOrder:=xlByColumns, SearchDirection:=xlPreviou
If LastColm.Column <> 255 Then
aWS.Range(aWS.Columns(Last
End If
RowCount = aWS.UsedRange.Rows.Count 'Reset scroll bars and last cell
mWB.Sheets(1).Select 'select first data sheet on master workbook
Application.EnableEvents = True 're-enable events
Application.ScreenUpdating
'Clear memory of the object variables
Set tWB = Nothing
Set tWS = Nothing
Set mWB = Nothing
Set aWS = Nothing
Set uRange = Nothing
Set LastColm = Nothing
End Sub
Matt
Main Topics
Browse All Topics





by: matthewspatrickPosted on 2005-10-04 at 13:43:03ID: 15017590
Hi Bud,
= False
).Copy ).Copy After:=Dest.Worksheets(Des t.Workshee ts.Count)
= True
Try this. It assumes that none of the 100 workbooks are open, and that it will save a Summary.xls to the same directory:
Sub Summarize()
Dim Counter As Long
Dim Source As Workbook
Dim Dest As Workbook
Const MyDir As String = "c:\temp\"
Application.ScreenUpdating
For Counter = 1 To 100
Set Source = Workbooks.Open(MyDir & "Book" & Counter & ".xls")
If Counter = 1 Then
Source.Worksheets("Sheet1"
Set Dest = ActiveWorkbook
Else
Source.Worksheets("Sheet1"
End If
Source.Close False
Next
Dest.SaveAs MyDir & "Summary.xls"
Application.ScreenUpdating
MsgBox "Done"
End Sub
Regards,
Patrick