Naming sheets with workbook name after merging? - excel

I've got over 200 workbooks that I need to merge, the code below will merge the workbooks and add all the sheets into one workbook.
In that workbook the sheets are being named Sheet 1 (1), Sheet 1 (2) and so on.
If the sheet was copied from Workbook1 the sheet name would be workbook 1
Sub mergeFiles()
'Merges all files in a folder to a main file.
'Define variables:
Dim numberOfFilesChosen, i As Integer
Dim tempFileDialog As FileDialog
Dim mainWorkbook, sourceWorkbook As Workbook
Dim tempWorkSheet As Worksheet
Set mainWorkbook = Application.ActiveWorkbook
Set tempFileDialog = Application.FileDialog(msoFileDialogFilePicker)
'Allow the user to select multiple workbooks
tempFileDialog.AllowMultiSelect = True
numberOfFilesChosen = tempFileDialog.Show
'Loop through all selected workbooks
For i = 1 To tempFileDialog.SelectedItems.Count
'Open each workbook
Workbooks.Open tempFileDialog.SelectedItems(i)
Set sourceWorkbook = ActiveWorkbook
'Copy each worksheet to the end of the main workbook
For Each tempWorkSheet In sourceWorkbook.Worksheets
tempWorkSheet.Copy after:=mainWorkbook.Sheets(mainWorkbook.Worksheets.Count)
Next tempWorkSheet
'Close the source workbook
sourceWorkbook.Close
Next i
End Sub

Add this in you For Each loop
Dim j as integer ‘Add to top of your sub
j = 0 ‘Add inside for loop
For Each tempWorkSheet In sourceWorkbook.Worksheets
j= j+1
tempWorkSheet.Copy after:=mainWorkbook.Sheets(mainWorkbook.Worksheets.Count)
ActiveSheet.Name = sourceWorkBook.Name & “ - “ & j ‘Added Line of code to rename copied tab
Next tempWorkSheet
As long as your workbook names aren’t too long or duplicate, it should be good

Merge Files
Code Issues
You have declared numberOfFilesChosen as Variant:
Dim numberOfFilesChosen, i As Integer ' Wrong
Dim numberOfFilesChosen as Integer, i As Integer ' OK
You have declared mainWorkbook as Variant:
Dim mainWorkbook, sourceWorkbook As Workbook ' Wrong
Dim mainWorkbook as Workbook, sourceWorkbook As Workbook ' OK
Such a code should be in the Workbook (mainWorkbook) where the
Worksheets are being imported, so you don't need a variable, just use
ThisWorkbook. Then in combination with the With statement, you
can use e.g. .Sheets(.Sheets.Count).
You are changing between sheets and worksheets. When you use mainWorkbook.Worksheets.Count, this might not necessarily be the last sheet, so it would be more correct to use mainWorkbook.Sheets.Count especially for the added sheet counter to function correctly.
tempWorkSheet.Copy after:=mainWorkbook.Sheets(mainWorkbook.Worksheets.Count)
tempWorkSheet.Copy after:=mainWorkbook.Sheets(mainWorkbook.Sheets.Count) ' Preferable
When you use sourceWorkbook.Close, you might be asked to save the workbook. Using
sourceWorkbook.Close False ' Preferable
will close the workbook without saving changes.
The code will fail if you run it another time, because the sheet names
it will try to create are the same. Therefore I have added
DeleteWorksheetsExceptOne which I used while testing the code.
The Code
Sub mergeFiles()
'Merges all files in a folder to a main file.
'Define variables:
Dim tempFileDialog As FileDialog
Dim sourceWorkbook As Workbook
Dim tempWorkSheet As Worksheet
Dim numberOfFilesChosen As Long, i As Long, j As Long
Set tempFileDialog = Application.FileDialog(msoFileDialogFilePicker)
'Allow the user to select multiple workbooks
tempFileDialog.AllowMultiSelect = True
numberOfFilesChosen = tempFileDialog.Show
With ThisWorkbook
'Loop through all selected workbooks
For i = 1 To tempFileDialog.SelectedItems.Count
'Open each workbook
Workbooks.Open tempFileDialog.SelectedItems(i)
Set sourceWorkbook = ActiveWorkbook
j = 0
'Copy each worksheet to the end of the main workbook
For Each tempWorkSheet In sourceWorkbook.Worksheets
j = j + 1
tempWorkSheet.Copy After:=.Sheets(.Sheets.Count)
' Rename newly added worksheet to the name of Source Workbook
' concatenated with "-" and Counter (j).
.Sheets(.Sheets.Count).Name = sourceWorkbook.Name & "-" & j
Next
'Close the source workbook. False for not saving changes.
sourceWorkbook.Close False
Next
End With
End Sub
Delete All Worksheets But One
'*******************************************************************************
' Purpose: Deletes all Worksheets in the ActiveWorkbook except one.
' Danger: This code doesn't ask anything, it just does. In the end you will
' end up with just one worksheet (cStrWsExcept) in the workbook
' (cStrWbPath). If you have executed this code and the result is not
' satisfactory, just close the workbook and try again or don't. There
' will be no alert like "Do you want to save ..." because of the line:
' ".Saved = True" i.e. "objWb.Saved = True".
' Arguments (As Constants):
' cStrWbPath
' The path of the workbook to be processed. If "", then ActiveWorkbook is
' used.
' cStrWsExcept
' The worksheet not to be deleted. If "", then the Activesheet is used.
'*******************************************************************************
Sub DeleteWorksheetsExceptOne()
Const cStrWbPath = "" ' if "" then ActiveWorkbook
Const cStrWsExcept = "Sheet1" ' if "" then ActiveSheet
Dim objWb As Workbook
Dim objWsExcept As Worksheet
Dim objWsDelete As Worksheet
If cStrWbPath = "" Then
Set objWb = ActiveWorkbook
Else
Set objWb = Workbooks(cStrWbPath)
End If
With objWb
If cStrWsExcept = "" Then
Set objWsExcept = .ActiveSheet
Else
Set objWsExcept = .Worksheets(cStrWsExcept)
End If
' To suppress the "Data may exist in the sheet(s) selected for deletion.
' To permanently delete the data, press Delete." - Alert:
Application.DisplayAlerts = False
For Each objWsDelete In .Worksheets
If objWsDelete.Name <> objWsExcept.Name Then
objWsDelete.Delete
End If
Next
' To suppress the "Do you want to save changes you made to ... ?" - Alert:
.Saved = True
Application.DisplayAlerts = True
End With
End Sub
'*******************************************************************************

Related

Copy Worksheets From One Workbook into Another

I would like to copy all the sheets from one workbook other than the first sheet to another workbook and then once the sheets have been copied to the other workbook, close it. Here is what I have for my code so far. Right now, it is copying all the sheets from Workbook SD09_KW.xlsm and pasting them into the Workbook Pending SD09 which is the correct location. However, I would like it to copy all the worksheets in SD09_KW other than the first sheet. It also is making copies of all the sheets in Pending SD09 and saving them inside the workbook too which I do not want it to do. Any help would be great! Thanks.
Sub CopySD()
Dim DataWorkbook As Workbook, PendingWorkbook As Workbook, Sheet As Worksheet
Set DataWorkbook = Workbooks.Open("C:\Users\Documents\SD09_KW.xlsm")
Set PendingWorkbook = Workbooks("Pending SD09")
For Each Sheet In DataWorkbook.Sheets
Sheets.copy After:=PendingWorkbook.Sheets(PendingWorkbook.Worksheets.Count)
Next
PendingWorkbook.Close SaveChanges:=True
End Sub
UPDATE: I have made a few changes, however, nwo I am getting an error of "Object doesn't support this property or method" in line DataWorkbook(i).Sheets.copy After:=PendingWorkbook.Sheets(PendingWorkbook.Worksheets.Count)
Here is the updated code:
Sub CopySD(sysnum As String) ' copies over the SD tabs created into the pending doc workbook
Dim DataWorkbook As Workbook, PendingWorkbook As Workbook, i As Long
Set DataWorkbook = Workbooks.Open("C:\Users\Documents\SD093_KW.xlsm")
Set PendingWorkbook = Workbooks.Open("C:\Users\Documents\Pending SD093.xlsx")
For i = 2 To DataWorkbook.Worksheets.Count
If Not SheetExists(sysnum, PendingWorkbook) Then ' SD tab not already saved to pending workbook
DataWorkbook.Sheets(i).copy After:=PendingWorkbook.Sheets(PendingWorkbook.Worksheets.Count) ' save SD tab to pending workbook
End If
Next
PendingWorkbook.Close SaveChanges:=True
Application.DisplayAlerts = False
For i = DataWorkbook.Sheets.Count To 2 Step -1
DataWorkbook.Sheets(i).Delete
Next i
Application.DisplayAlerts = True
End Sub
Copy/Delete Worksheets
Note the mistakes in your codes where you have used:
Sheets.Copy... instead of
Sheet.Copy...
DataWorkbook(i).Sheets.copy... instead of
DataWorkbook.Worksheets(i).Copy...
Note the inaccuracies (inconsistencies, mistakes if the workbooks contain charts) in your codes where you have used:
... In DataWorkbook.Sheets instead of
... In DataWorkbook.Worksheets
... After:=PendingWorkbook.Sheets(PendingWorkbook.Worksheets.Count) instead of
... After:=PendingWorkbook.Sheets(PendingWorkbook.Sheets.Count)
... To DataWorkbook.Sheets.Count instead of
... To DataWorkbook.Worksheets.Count
Copy All But the First Worksheet
For Each...Next
Sub CopyForEach()
Dim DataWorkbook As Workbook
Set DataWorkbook = Workbooks.Open("C:\Users\Documents\SD09_KW.xlsm")
Dim PendingWorkbook As Workbook
Set PendingWorkbook = Workbooks.Open("C:\Users\Documents\Pending SD09.xlsx")
Dim ws As Worksheet
Dim FoundFirst As Boolean
For Each ws In DataWorkbook.Worksheets
If FoundFirst Then
ws.Copy After:=PendingWorkbook.Sheets(PendingWorkbook.Sheets.Count)
Else ' skip first
FoundFirst = True
End If
Next ws
PendingWorkbook.Close SaveChanges:=True
End Sub
For...Next
Sub CopyForNext()
Dim DataWorkbook As Workbook
Set DataWorkbook = Workbooks.Open("C:\Users\Documents\SD09_KW.xlsm")
Dim PendingWorkbook As Workbook
Set PendingWorkbook = Workbooks.Open("C:\Users\Documents\Pending SD09.xlsx")
Dim i As Long
For i = 2 To DataWorkbook.Worksheets.Count ' 2 means skip first
DataWorkbook.Worksheets(i).Copy _
After:=PendingWorkbook.Sheets(PendingWorkbook.Sheets.Count)
Next i
PendingWorkbook.Close SaveChanges:=True
End Sub
Delete All But the First Worksheet
After PendingWorkbook.Close SaveChanges:=True you will use either
DeleteAllButFirstWorksheetForEachNext DataWorkbook
or
DeleteAllButFirstWorksheetForNext DataWorkbook
Of course, use shorter names for the procedures e.g. DeleteAllButFirst.
For Each...Next
Sub DeleteAllButFirstWorksheetForEachNext(ByVal wb As Workbook)
Dim wsCount As Long: wsCount = wb.Worksheets.Count
If wsCount < 2 Then Exit Sub
Dim WorksheetNames() As String: ReDim WorksheetNames(1 To wsCount - 1)
Dim ws As Worksheet
Dim n As Long
Dim FoundFirst As Boolean
For Each ws In wb.Worksheets
If FoundFirst Then
n = n + 1
WorksheetNames(n) = ws.Name
Else ' skip first
FoundFirst = True
End If
Next ws
Application.DisplayAlerts = False
wb.Worksheets(WorksheetNames).Delete
Application.DisplayAlerts = True
End Sub
For...Next
Sub DeleteAllButFirstWorksheetForNext(ByVal wb As Workbook)
Dim wsCount As Long: wsCount = wb.Worksheets.Count
If wsCount < 2 Then Exit Sub
Dim WorksheetNames() As String: ReDim WorksheetNames(1 To wsCount - 1)
Dim i As Long
Dim n As Long
For i = 2 To wsCount
WorksheetNames(i - 1) = wb.Worksheets(i).Name
Next i
Application.DisplayAlerts = False
wb.Worksheets(WorksheetNames).Delete
Application.DisplayAlerts = True
End Sub

Macro to Copy/Paste data from txt files into new workbook is pasting into the Personal Workbook

The title more or less sums it up. I am trying to make a simple macro to open and then copy/paste data from txt files into a newly opened workbook. What I have work but it pastes it into the Personal Workbook at that opens before a new "Book1". I could make it paste to Book1 but I just want it paste it into the first workbook opened in case I want to add more data to the same file down the road. I am pretty new to this so any help would be greatly appreciated!!
Public Sub ImportFile()
Dim TextFile As Workbook
Dim OpenFiles() As Variant
Dim x As Integer
OpenFiles = Application.GetOpenFilename(Title:="Select File(s) to Import", MultiSelect:=True)
For x = 1 To Application.CountA(OpenFiles)
Set TextFile = Workbooks.Open(OpenFiles(x))
TextFile.Sheets(1).Range("A1").CurrentRegion.Copy
Workbooks(1).Activate
Workbooks(1).Worksheets.Add
ActiveSheet.Paste
TextFile.Close
Next x
End Sub
It will create a new workbook. It will create additional worksheets with names "New1, New2 " etc. Hope it will help your requirement
Public Sub ImportFile()
Dim TextFile As Workbook
Dim OpenFiles() As Variant
Dim x As Integer
Dim wb As Workbook
Dim ws As Worksheet
Set wb = Workbooks.Add
OpenFiles = Application.GetOpenFilename(Title:="Select File(s) to Import", MultiSelect:=True)
For x = 1 To Application.CountA(OpenFiles)
i = 1
Set ws = wb.Sheets.Add
Set TextFile = Workbooks.Open(OpenFiles(x))
TextFile.Sheets(1).Range("A1").CurrentRegion.Copy
ws.Paste
Application.CutCopyMode = False
Application.CutCopyMode = True
ws.Name = "New" & i
i = i + 1
TextFile.Close
Next x
End Sub
Try the next code line, please:
Debug.Print Workbooks(1).Name
It will return "PERSONAL.XLSB".
So, if you use your code like it is, it will all the time copy in "PERSONAL.XLSB".
If you do not want using a new workbook and you like your initial approach, you can try:
'...
'your existing code
TextFile.Sheets(1).Range("A1").CurrentRegion.Copy
Dim wb As Workbook, ws As Worksheet
If Workbooks.count > 1 Then
Set wb = Workbooks(2)
Else
Set wb = Workbooks.Add
End If
Set ws = wb.Worksheets.Add
'and always use this sheet to copy on it...
'your existing code...
'...

Combine sheets from different workbooks with same name

I'm trying to solve an issue i'm currently dealing with.
Below you'll find the issue:
I'm having multiple excel sheets that I'd like to merge into one file (located into different workbooks).
Each workbook consists out of the same sheets (SHEET1, SHEET2, SHEET3).
I'd like to merge all workbooks into 1 masterfile - and want to keep the same structure (SHEET1 = all date form all sheets).
So far I've manged to solve the merging issue with the below code:
Sub mergeFiles()
Dim numberOfFilesChosen, i As Integer
Dim tempFileDialog As FileDialog
Dim mainWorkbook, sourceWorkbook As Workbook
Dim tempWorkSheet As Worksheet
Set mainWorkbook = Application.ActiveWorkbook
Set tempFileDialog = Application.FileDialog(msoFileDialogFilePicker)
tempFileDialog.AllowMultiSelect = True
numberOfFilesChosen = tempFileDialog.Show
For i = 1 To tempFileDialog.SelectedItems.Count
Workbooks.Open tempFileDialog.SelectedItems(i)
Set sourceWorkbook = ActiveWorkbook
For Each tempWorkSheet In sourceWorkbook.Worksheets
tempWorkSheet.Copy after:=mainWorkbook.Sheets(mainWorkbook.Worksheets.Count)
Next tempWorkSheet
sourceWorkbook.Close
Next i
End Sub
I navigate via de Application.FileDialog to the folder with the different sheets. I select the files i want to merge, and then VBA does its job, and merges the files into one Excel sheet.
Hence some of the sheets are having the same name (=always) = SHEET 1, SHEET 2, SHEET 3, the merged sheets are having the same name with a figure behind (= SHEET1 (1), SHEET1 (2) ...)
I've managed to merge all the sheets into one worksheet, using the below code - but i can't mange to add a restriction to it - e.g. merge all the sheets starting with (SHEET1* into MASTERDATA SHEET1, SHEET2 * into MASTERDATA SHEET2, SHEET3 * into MASTERDATA SHEET3)
Sub Merge_Sheets()
Sheets.Add
ActiveSheet.Name = "MASTERDATA"
For Each ws In Worksheets
ws.Activate
If ws.Name <> "MASTERDATA" Then
ws.UsedRange.Select
Selection.Copy
Sheets("MASTERDATA").Activate
ActiveSheet.Range("A1048576").Select
Selection.End(xlUp).Select
If ActiveCell.Address <> "$A$1" Then
ActiveCell.Offset(1, 0).Select
End If
ActiveSheet.Paste
End If
Next
End Sub
Could any of you help me out + explain briefly the next step?
Kind Regards
D
You should check if the sheet name already exists in mainWorkbook. If it does append that data to the end of that sheet rather than insert a new worksheet. Therefore, you do not need to the second code
Try this (not tested and you might need to debug it, also note comments starting with '*)
Sub mergeFiles()
'* declare the type for each variable (no just at the end of the line)
'* always use Long if you're tempted to use Integer
Dim numberOfFilesChosen As Long, i As Long
Dim tempFileDialog As FileDialog
Dim mainWorkbook As Workbook, sourceWorkbook As Workbook
Dim tempWorkSheet As Worksheet
'* declare the destination sheet
Dim destWorkSheet As Worksheet
Set mainWorkbook = ThisWorkbook ' Application.ActiveWorkbook
Set tempFileDialog = Application.FileDialog(msoFileDialogFilePicker)
tempFileDialog.AllowMultiSelect = True
numberOfFilesChosen = tempFileDialog.Show
For i = 1 To tempFileDialog.SelectedItems.Count
'* you can set sourceWorkbook directly here
Set sourceWorkbook = Workbooks.Open(tempFileDialog.SelectedItems(i))
'Set sourceWorkbook = ActiveWorkbook
On Error Resume Next
For Each tempWorkSheet In sourceWorkbook.Worksheets
Set destWorkSheet = mainWorkbook.Sheets(tempWorkSheet.Name)
If Err.Number > 0 Then '* worksheet doesn't exist in mainWorkbook
tempWorkSheet.Copy after:=mainWorkbook.Sheets(mainWorkbook.Worksheets.Count)
Err.Clear
Else '* worksheet already exists
With tempWorkSheet.UsedRange
.Copy Destination:=destWorkSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1)
'* If you only want to copy the values remove the above line and uncomment the below line
'destWorkSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1).Resize(.Rows.Count, .Columns.Count).Value = .Value
End With
End If
Next tempWorkSheet
On Error GoTo 0
sourceWorkbook.Close
Next i
End Sub

Copy two worksheets into different workbook replacing current data

I modified code from Copy worksheet into different workbook replacing current data.
If I deselect range or I have selected different cell than A1, code falls into 1004 error.
Sub TG_update()
Dim wb1 As Workbook, wb2 As Workbook, ws1Format As Worksheet, ws2Format As Worksheet, ws3Format As Worksheet, ws4Format As Worksheet
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set wb1 = ThisWorkbook
Set wb2 = Workbooks.Open("[add your path.xlsx]")
Set ws1Format = wb1.Sheets("SheetA1")
Set ws2Format = wb2.Sheets("SheetB1")
Set ws3Format = wb1.Sheets("SheetA2")
Set ws4Format = wb2.Sheets("SheetB2")
'' Copy the cells of the "Format" worksheet.
ws2Format.Cells.Copy
'' Paste cells to the sheet "Format".
wb1.Sheets("SheetA1").Paste
ws4Format.Cells.Copy
wb1.Sheets("SheetB1").Paste
wb2.Close False 'remove false if you want to be asked if the workbook shall be saved.
wb1.Sheets("Store").Activate
Application.ScreenUpdating = True
Application.DisplayAlerts = True
MsgBox "Date successfully updated"
End Sub
Please try this code. Instead of copying and pasting millions of blank cells this code copies the worksheet from the source and pastes it to the workbook with the code. If the action is successful the old sheet is deleted. The final report alerts about errors if sheets weren't found.
Sub TG_update()
' 016
Dim Wb As Workbook ' ThisWorkbook
Dim WbS As Workbook ' Source
Dim Ffn As String ' Full FileName
Dim Ws As Worksheet
Dim TabName() As String
Dim i As Integer ' TabName index
Dim n As Integer ' tab counter
Set Wb = ThisWorkbook
' specify the workbook to be copied from: Full path and name
Ffn = "F:\AWK PC\Drive E (Archive)\PVT Archive\Class 1\1-2018 (Jan 2020)\TXL 180719 Z Distance.xlsm"
' enumerate the sheet names in CSV format (sheets must exist in Wb)
TabName = Split("SheetA1,SheetB1", ",")
With Application
.ScreenUpdating = False
.DisplayAlerts = False
End With
Set WbS = Workbooks.Open(Ffn)
For i = 0 To UBound(TabName)
On Error Resume Next ' suppress error if worksheet isn't found
WbS.Worksheets(TabName(i)).Copy After:=Wb.Worksheets(Wb.Worksheets.Count)
If Err.Number = 0 Then
n = n + 1
End If
Next i
WbS.Close SaveChanges:=False
On Error GoTo 0
For i = 0 To UBound(TabName)
For Each Ws In Wb.Worksheets
If InStr(Ws.Name, TabName(i) & " (") = 1 Then
Wb.Worksheets(TabName(i)).Delete
Ws.Name = TabName(i)
End If
Next Ws
Next i
With Application
.ScreenUpdating = True
.DisplayAlerts = True
End With
MsgBox n & " of " & i & " worksheets were successfully updated.", _
vbInformation, "Action report"
End Sub
Creative names like Wb1, Wb2, Ws1, Ws2, SheetA1, SheetA2 represent the punishment imaginative programmers inflict on those who come after them to correct their hastily concocted code. Give your VBA project a better reputation by bestowing names on your two worksheets that permit their identification.

Merge multiple xlsx and xls files into one workbook

I have many xlsx and xls files in a folder containing 2-3 worksheets in each file. I want to merge all these files into one workbook. I have a sample code but it is not merging xlsx file, it is picking only xls files of the selected folder. Sample code is mentioned below. Help me
Sub MergeFiles ()
Dim numberOfFilesChosen, i As Integer
Dim tempFD As FileDialog
Dim mainWb, sourceWb As Workbook
Dim tempWS As Worksheet
Set mainWb = Workbooks.Add 'Application.ActiveWorkbook
Set tempFD = Application.FileDialog(msoFileDialogFilePicker)
'Allow the user to select multiple workbooks
tempFD.AllowMultiSelect = True
numberOfFilesChosen = tempFD.Show
'Loop through all selected workbooks
For i = 1 To tempFD.SelectedItems.Count
'Open each workbook
Workbooks.Open tempFD.SelectedItems(i)
Set sourceWb = ActiveWorkbook
'Copy each worksheet to the end of the main workbook
For Each tempWS In sourceWb.Worksheets
tempWS.Copy after:=mainWb.Sheets(mainWb.Worksheets.Count)
Next tempWS
'Close the source workbook
sourceWb.Close
Next i
End Sub
Your code does work without any problems with a small amount of xls, xlsx and xlsb files.
I've tried with 24 different files.
Sub MergeFiles()
Application.ScreenUpdating = False ' **** Gain some performance?
Dim numberOfFilesChosen, i As Integer
Dim tempFD As FileDialog
Dim mainWb, sourceWb As Workbook
Dim tempWS As Worksheet
Set mainWb = Workbooks.Add 'Application.ActiveWorkbook
Set tempFD = Application.FileDialog(msoFileDialogFilePicker)
'Allow the user to select multiple workbooks
tempFD.AllowMultiSelect = True
numberOfFilesChosen = tempFD.Show
'Loop through all selected workbooks
For i = 1 To tempFD.SelectedItems.Count
'Open each workbook
Workbooks.Open tempFD.SelectedItems(i)
Set sourceWb = ActiveWorkbook
' Application.ScreenUpdating = True '****** Uncomment to get more feedback
' mainWb.Activate
' mainWb.Sheets(1).Range("A1").EntireRow.Insert
' mainWb.Sheets(1).Range("A1").Value = sourceWb.Name
' Debug.Print sourceWb.Name
' Application.ScreenUpdating = True
'Copy each worksheet to the end of the main workbook
For Each tempWS In sourceWb.Worksheets
tempWS.Copy after:=mainWb.Sheets(mainWb.Worksheets.Count)
Next tempWS
'Close the source workbook
sourceWb.Close
Next i
Application.ScreenUpdating = True ' **** Gain some performance?
End Sub

Resources