have this code:
Sub MergeSheets()
Dim SrcBook As Workbook
Dim TrgtBook As Workbook
Dim fso As Object
Dim f As Object
Dim ff As Object
Dim i As Long
Dim SrcLCell
Dim TrgtLCell
Application.ScreenUpdating = False '<--- Stops Screenflicker
Application.DisplayAlerts = False '<--- Stops annoying Excel pop-up questions
Set TrgtBook = ThisWorkbook
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.Getfolder("C:\Documents and Settings\bestjon1\Desktop\Combine Sheets")
Set ff = CreateObject("Scripting.FileSystemObject")
For Each ff In f.Files
If ff.Name Like "*.xls" Then '<----- checks if it is an excel file, amend if necessary with 2007 extensions (Or ff.name like "*.xlsm" Or ...)
Workbooks.Open Filename:=f & "\" & ff.Name
Set SrcBook = ActiveWorkbook
For i = 1 To SrcBook.Sheets.Count
SrcBook.Sheets(i).UsedRange
TrgtBook.ActiveSheet.UsedRange
SrcLCell = SrcBook.Sheets(i).Cells(SrcBook.Sheets(i).Cells.SpecialCells(xlLastCell).Row, SrcBook.Sheets(i).Cells.SpecialCells(xlLastCell).Column).Address
If TrgtBook.ActiveSheet.Cells.SpecialCells(xlLastCell).Row > 1 Then
TrgtLCell = TrgtBook.ActiveSheet.Cells(TrgtBook.ActiveSheet.Cells.SpecialCells(xlLastCell).Row + 1, 1).Address
Else
TrgtLCell = TrgtBook.ActiveSheet.Cells(TrgtBook.ActiveSheet.Cells.SpecialCells(xlLastCell).Row, 1).Address
End If
SrcBook.Sheets(i).Range("A1:" & SrcLCell).Copy
TrgtBook.Sheets(1).Range(TrgtLCell).PasteSpecial xlPasteValues '<--- pastes only the values, all formulas, formating etc is lost, remove / amend as necessary
Application.CutCopyMode = False
Next i
SrcBook.Saved = True
Workbooks(ff.Name).Close
End If
Next ff
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Need small change: What's the best way to make this work for select sheets in each file that I am using to combine? right now it works for all sheets in each file and I only want specific ones. Thanks,
Sub MergeSheets()
Dim SrcBook As Workbook
Dim TrgtBook As Workbook
Dim fso As Object
Dim f As Object
Dim ff As Object
Dim i As Long
Dim SrcLCell
Dim TrgtLCell
Application.ScreenUpdating = False '<--- Stops Screenflicker
Application.DisplayAlerts = False '<--- Stops annoying Excel pop-up questions
Set TrgtBook = ThisWorkbook
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.Getfolder("C:\Documents and Settings\bestjon1\Desktop\Combine Sheets")
Set ff = CreateObject("Scripting.FileSystemObject")
For Each ff In f.Files
If ff.Name Like "*.xls" Then '<----- checks if it is an excel file, amend if necessary with 2007 extensions (Or ff.name like "*.xlsm" Or ...)
Workbooks.Open Filename:=f & "\" & ff.Name
Set SrcBook = ActiveWorkbook
For i = 1 To SrcBook.Sheets.Count
SrcBook.Sheets(i).UsedRange
TrgtBook.ActiveSheet.UsedRange
SrcLCell = SrcBook.Sheets(i).Cells(SrcBook.Sheets(i).Cells.SpecialCells(xlLastCell).Row, SrcBook.Sheets(i).Cells.SpecialCells(xlLastCell).Column).Address
If TrgtBook.ActiveSheet.Cells.SpecialCells(xlLastCell).Row > 1 Then
TrgtLCell = TrgtBook.ActiveSheet.Cells(TrgtBook.ActiveSheet.Cells.SpecialCells(xlLastCell).Row + 1, 1).Address
Else
TrgtLCell = TrgtBook.ActiveSheet.Cells(TrgtBook.ActiveSheet.Cells.SpecialCells(xlLastCell).Row, 1).Address
End If
SrcBook.Sheets(i).Range("A1:" & SrcLCell).Copy
TrgtBook.Sheets(1).Range(TrgtLCell).PasteSpecial xlPasteValues '<--- pastes only the values, all formulas, formating etc is lost, remove / amend as necessary
Application.CutCopyMode = False
Next i
SrcBook.Saved = True
Workbooks(ff.Name).Close
End If
Next ff
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Need small change: What's the best way to make this work for select sheets in each file that I am using to combine? right now it works for all sheets in each file and I only want specific ones. Thanks,