Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combine several Excel worksheets from workbooks into one via Codes 1

Status
Not open for further replies.

UongSaki

Technical User
Nov 15, 2003
65
0
0
US
I heave several Excel 2000 workbooks and each workbook has several worksheets of similar columns. Is there a way in Excel that I can combine all workbooks and their worksheets into one worksheet without having to copy and paste each worksheet?

Thank you,

UongSaki
 
Kind of ugly but it works.

Code:
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim Path As String
Dim Ext As String
Dim fName As String
Dim sht As Worksheet
Workbooks.Add
With ActiveWorkbook
.SaveAs "MergeExcelFiles"
End With
Ext = ".xls"
Path = "C:\"
fName = Dir(Path, vbNormal)
    Do While fName <> ""
            If Right(fName, 4) = Ext Then
             Workbooks.Open Filename:=Path & fName
                With ActiveWorkbook
                    For Each sht In .Sheets
                        sht.Range("A1", sht.Range("A1").SpecialCells(xlLastCell)) _
                            .Copy Workbooks("MergeExcelFiles.xls").Sheets("Sheet2") _
                            .Range("A" & Workbooks("MergeExcelFiles.xls").Sheets("Sheet2") _
                            .Range("A1").SpecialCells(xlLastCell).Row)
                    Next
                    .Close
                End With
            End If
        fName = Dir
    Loop
    
Application.ScreenUpdating = True
Application.DisplayAlerts = True


You will have to set the variable path to the directory that contains your files.

HTH,
Eric
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top