Merging workbooks into a new file - excel

I'm trying to merge multiple Excel files from one Folder into a new file. I've found a solution on the Internet, that is adding my files into an open one. I'm not really into VBA Excel, so I think it's a basic problem, but I can't do it, things I've tried haven't worked properly. I would like to change the following code to create a new file called "summary" in the "Path" and copy the Sheets into this new file, overwriting the file every time I do it and deleting the several source files after doing this.
Is there a possibility of merging all those files into one without opening everyone of it?
Sub GetSheets()
Path = "C:\Merging\"
FileName = Dir(Path & "*.xls")
Do While FileName <> ""
Workbooks.Open FileName:=Path & FileName, ReadOnly:=True
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Next Sheet
Workbooks(FileName).Close
FileName = Dir()
Loop
End Sub

Your code almost works as is, just needs a couple of slight tweaks. I also agree with #AnalystCave that if this is a repeating exercise, you may consider a more streamlined solution. But this will work for you.
EDIT: changed to deal with existing destination file -- if it exists and is open, then connect to it otherwise open it; then delete all sheets in the existing file to prepare for the copies
Option Explicit
Function IsSheetEmpty(sht As Worksheet) As Boolean
IsSheetEmpty = Application.WorksheetFunction.CountA(sht.Cells) = 0
End Function
Sub GetSheets()
Dim Path, Filename As String
Dim Sheet As Worksheet
Dim newBook As Workbook
Dim appSheets As Integer
Dim srcFile As String
Dim dstFile As String
Dim dstPath As String
Dim wasntAlreadyOpen As Boolean
Application.ScreenUpdating = False 'go faster by not waiting for display
'--- create a new workbook with only one worksheet
dstFile = "AllSheetsHere.xlsx"
dstPath = ActiveWorkbook.Path & "\" & dstFile
wasntAlreadyOpen = True
If Dir(dstPath) = "" Then
'--- the destination workbook does not (yet) exist, so create it
appSheets = Application.SheetsInNewWorkbook 'saves the default number of new sheets
Application.SheetsInNewWorkbook = 1 'force only one new sheet
Set newBook = Application.Workbooks.Add
newBook.SaveAs dstFile
Application.SheetsInNewWorkbook = appSheets 'restores the default number of new sheets
Else
'--- the destination workbook exists, so ...
On Error Resume Next
wasntAlreadyOpen = False
Set newBook = Workbooks(dstFile) 'connect if already open
If newBook Is Nothing Then
Set newBook = Workbooks.Open(dstPath) 'open if needed
wasntAlreadyOpen = True
End If
On Error GoTo 0
'--- make sure to delete any/all worksheets so we're only left
' with a single empty sheet named "Sheet1"
Application.DisplayAlerts = False 'we dont need to see the warning message
Do While newBook.Sheets.Count > 1
newBook.Sheets(newBook.Sheets.Count).Delete
Loop
newBook.Sheets(1).Name = "Sheet1"
newBook.Sheets(1).Cells.ClearContents
newBook.Sheets(1).Cells.ClearFormats
Application.DisplayAlerts = True 'turn alerts back on
End If
Path = "C:\Temp\"
Filename = Dir(Path & "*.xls?") 'add the ? to pick up *.xlsx and *.xlsm files
Do While Filename <> ""
srcFile = Path & Filename
Workbooks.Open Filename:=srcFile, ReadOnly:=True
For Each Sheet In ActiveWorkbook.Sheets
'--- potentially check for blank sheets, or only sheets
' with specific data on them
If Not IsSheetEmpty(Sheet) Then
Sheet.Copy After:=newBook.Sheets(1)
End If
Next Sheet
Workbooks(Filename).Close (False) 'add False to close without saving
Kill srcFile 'deletes the file
Filename = Dir()
Loop
'--- delete the original empty worksheet and save the book
If newBook.Sheets.Count > 1 Then
newBook.Sheets(1).Delete
End If
newBook.Save
'--- leave it open if it was already open when we started
If wasntAlreadyOpen Then
newBook.Close
End If
Application.ScreenUpdating = True 're-enable screen updates
End Sub

Firstly, regardless of your solution you will still need to OPEN every Excel workbook if you want to merge all of them.
Secondly, I think you might want to rephrase your question to "Is there a possibility of merging all those files into one faster or in any easier way?"
From the level of Excel VBA there is really no other way then opening each Workbook within the same Application level. If this is a one-time exercise I would stick to the code you already have and bear with it.
However, if this is an exercise you will be doing repeatedly and need an efficient solution, your only option is resorting to the OpenXML format which does not require a heavyweight Excel process e.g. creating a C# solution using the ClosedXML library. This will definitely reduce the time needed to consolidate your workbooks.

Related

How do I duplicate an excel workbook and all of the code inside it

I am having a bit of brain freeze moment. I need to copy a workbook including all of its VBA code. I then need to delete from the workbook a couple of sheets. I know that I can just create a new workbook and copy the sheets to that new wb however I need the code for the entire wb to go with it.
I also know that I could do a workbook save copy as.
However it will be random people saving it to their computer and so how do I know how to refer to this book in order to then delete the sheets?
I feel like it is so easy, but I don't know enough to figure it out.
Thanks for your help!
I believe you are worrying far to much about the fact that the file you want to copy is a workbook with all its macros, ...
You can, as far as the copying is concerned, simply treated as any other file, like this:
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile "C:\Temp_Folder\Test.txt", "C:\Temp_Folder\Test2.txt2"
This works for any kind of file (Excel files, included). Once you have done this, you can open the copied file and remove some sheets and so on.
You can copy this code to your new button_click routine
I tested it and it works for me.
Change Sheet3 and Sheet4 to meet your own needs.
The folder dialog code came from here.
Option Explicit
'Private Sub button_click()
Private Sub test73622125()
Dim fldr As FileDialog, fso As Object
Dim sFilename$, sItem$
Dim wb As Workbook
Dim WS As Worksheet
'create folder dialog
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
sFilename = ""
With fldr
.Title = "Please select a folder in which to create a copy of this workbook for a student"
.AllowMultiSelect = False
.InitialFileName = ThisWorkbook.Path
If .Show <> -1 Then
Call MsgBox("No folder selected. No copy created", vbOKOnly, "Warning")
Exit Sub
Else
'the new copy must have a different name so this code can open it immediately.
' Excel will not open two documents with the same name simultaneously
sFilename = .SelectedItems(1) & "\" & Replace(ThisWorkbook.Name, ".xlsm", " Copy.xlsm")
End If
End With
'copy file, courtesy of Dominique
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile ThisWorkbook.FullName, sFilename
'open newly copied document
Set wb = Workbooks.Open(FileName:=sFilename)
'change Sheet3 and 4 to the the sheets students should not see
For Each WS In wb.Sheets
If WS.Name = "Sheet3" Or WS.Name = "Sheet4" Then
Application.DisplayAlerts = False
WS.Delete
Application.DisplayAlerts = True
End If
Next
'save and close new copy for student
Application.DisplayAlerts = False
wb.Close (True)
Application.DisplayAlerts = True
End Sub

How do I take Excel files from my Chrome Downloads and simultaneously combine them as tabs in an Excel Workbook?

Currently I have to manually click on each file in my Chrome Downloads and drag them into a workbook as a tab. If I download 60+ Excel files, it's a lot. Is it possible to have them all join together in one workbook simultaneously? Is there a code/formula/operation?
I tried "Get Data--> From Files--> From Folder and combine and load the files, but they don't combine as separate tabs. I just see general data of all the files in one worksheet tab.
Note: im operating under the assumption each workbook you are downloading is filetype ".xlsx" and contains only 1 worksheet.
If the files are in your "Chrome Downloads" then they are also located in your computers download folder.
For me this folder is "C:\Users[your name]\Downloads". The first step is for you to figure out where these downloads are going.
Do these files have names following some sort of pattern?
If yes this will make the automation simpler.
Below I will layout a simple directory traversal using
pattern matching to select multiple files.
Specifically, every file of type ".xlsx" will have it's first worksheet pulled into the workbook containing the macro.
Sub getDownloads()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
' *** WHAT YOU NEED TO CHANGE ***
Dim directory_Of_Downloads As String: directory_Of_Downloads = "C:\Users\[your name probably]\Downloads"
Dim fileName As Variant: fileName = Dir(directory_Of_Downloads & "\*.xlsx", vbDirectory)
Dim wb As Workbook
While Not fileName = ""
Set wb = Workbooks.Open(directory_Of_Downloads & "\" & fileName, ReadOnly:=True)
wb.Sheets(1).Copy Before:=ThisWorkbook.Sheets(1)
' If the sheet name in the source workbook is a duplicate of a pre-existing worksheet in our current workbook
' then the sheet will end up being named "Sheet (#)"
On Error Resume Next
ThisWorkbook.Sheets(1).Name = wb.Sheets(1).Name
On Error GoTo 0
' close the workbook
wb.Close SaveChanges:=False
'Delete the workbook -> If you are sure uncomment the below line
'Kill directory_Of_Downloads & "\" & fileName
' set fileName = The next file located at directory_Of_Downloads which meets our search criteria -> ".xlsx"
fileName = Dir
Wend
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Best of luck!

How to Copy every sheet except sheet 1 and 2 on multiple excel workbook in one folder into another workbook

I really appreciate if someone here would help me crack this problem which i cant find the solution (and sorry for my bad english).
So i have multiple excels in one folder. every excel in it have same format 1st sheet for reference of every sheet, 2nd sheet for consolidation data, and 3rd sheet and the rest for the data to be consolidated. Every excel in the folder have various amount of sheet.
What i want to do is i want to copy data from range A27:AJ500 that begin from 3rd sheet to every sheet after, into another new workbook in sheet1 and paste it begin from cell A27 over and over into the bottom and looping for every excel in folder.
i dont have enough ability yet to write my own script but i managed to understand some and combine it into this script.
Sub Download_Data()
Path = "C:\Users\ASUS\Desktop\Done\"
Filename = Dir(Path & "*.xlsm")
'to open every excel in my folder
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True '--> i only managed to do it right till here
'supposed to copy range in every sheet of excel in my folder into different workbook
For Each ws In thiswoorkbook.Worksheets '--> i try write this code but i am confused to do what i want from here and i know this code is nowhere near true
With ws
If .Name <> "GABUNGAN" Then
range("A27:AJ500").Select
Selection.copy
Workbooks("Tes.xlsm").range("A27").PasteSpecial Paste:=xlPasteValues
End If
End With
Next ws
Workbooks(Filename).Close
Filename = Dir()
Loop
Application.Goto ActiveWorkbook.Sheets("sheet1").range("A1")
End Sub
I've been searching for the code not only i cant customize it to this code but also i cant understand what is wrong in the code therefore i write this question. Any help will be appreciated, thanks in advance for your attention wish you safe and sound.
Try this: (tested)
Dim sourcewb As Workbook
Dim destwb As Workbook
Dim y As Long
Dim ws As Worksheet
Dim strPath As String, strFilename As String
strPath = "C:\Users\ASUS\Desktop\Done\"
strFilename = Dir(strPath & "*.xlsm")
y = 27
Set destwb = ThisWorkbook
Do While strFilename <> ""
Set sourcewb = Workbooks.Open(Filename:=strPath & strFilename, ReadOnly:=True)
For Each ws In sourcewb.Worksheets
With ws
If .Name <> "name of reference sheet" And .Name <> "name of consolidation sheet" Then
.Range("A27:AJ500").Copy
destwb.Worksheets("sheet1").Range("A" & y).PasteSpecial Paste:=xlPasteValues
y = y + (500 - 27) + 1
End If
End With
Next ws
sourcewb.Close False
strFilename = Dir()
Loop

Excel VBA copying multiple sheets from multiple files in a folder into multiple sheets in one file

I have been trying to find a quick solution to "merge" excel files, came across at least a dozen different codes, tried recording my own macro and modifying that (instead of a range of sheet names trying to select the lot etc.) none of worked anywhere near the way I wanted it to, most of them didn't work at all.
The context as follows:
I have a lot of files in a folder ("C:\Zoltan\TEST\"), most have multiple sheets. I want to copy
all the sheets that DO NOT HAVE "Mailing" in the sheet name
from all the files that DO NOT HAVE "Printing" in the file name
into one file ("C:\Zoltan\TEST.xlsx") keeping the sheets separate as they are in the source files
only if the sheet name already exist, I want to give it a date stamp (e.g. the sheet called "NTI UK (150)" from "E8795 NTI Mailing Order.XLSX" created on the 28th August 2105 to become "NTI UK (150) 20150828"
Below is as far as I got, which unfortunately doesn't seem to do anything. Currently I'm running the macro from the opened destination file (which is not much of an issue, I'm quite happy to have it that way). I'm also still missing the "Printing" exclusion (instead of the "Mailing" inclusion) in the FILE NAME and the whole date stamp bit, but those will be my next steps:
Sub CombineSheets()
Dim sPath As String
Dim sFname As String
Dim wbk As Workbook
Dim wSht As Worksheet
Application.EnableEvents = False
Application.ScreenUpdating = False
sPath = "C:\Zoltan\TEST\"
ChDir sPath
sFname = "*Mailing*"
sFname = Dir(sPath & "\" & sFname & ".xl*", vbNormal)
Do Until sFname = ""
Set wbk = Workbooks.Open(sFname)
Windows(sFname).Activate
For Each ws In Sheets
If Not ws.Name Like "*Mailing*" Then ws.Copy Before:=ThisWorkbook.Sheets(1)
wbk.Close False
sFname = Dir()
Next
Loop
ActiveWorkbook.Save
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
I hope I've drawn up the problem in a clear and easy to understand way, but just to be on the safe side, if I did this manually it would be the following:
Open the TEST file
Open a source file that does not have the word
"Printing" in the file name
Highlight all the sheets that do not
have "Mailing" in the sheet name (these are just forms, not data
sheets, I don't need them)
Right click and "Move or Copy", tick
"Create a copy", select TEST.xlsx and select Sheet1
Close source file and move on to the next one
Please note, if the above is pure butchery, that is due to my lack of sufficient VBA skills. I tend to look at other people's codes, or record macros, take them apart and then try to make sense of them and put them back together the way I want them to work.
Where do I go wrong? Is there an easier way to code this? NB. I'd much rather copy entire sheets than highlighting ranges within sheets and place those ranges into new sheets in the destination file, like most codes (that I have come across) would do.
Many thankZ
Is your code creating a copy of the sheets you find? You're not explaining exactly what is, or is not, happening. You do have your sFname = Dir() command in the wrong place... And I suggest just automatically setting the names rather than trying to see if a name exists...
The filename is easy enough, use the following within your do loop:
Set wbk = Workbooks.Open(sFname)
Windows(sFname).Activate
For Each ws In Sheets
If Not sFname Like "*printing*" Then
If Not ws.Name Like "*Mailing*" Then
ws.Copy Before:=ThisWorkbook.Sheets(1)
ThisWorkbook.Sheets(1).Name = ThisWorkbook.Sheets(1).Name + Format(Now(), "yyyyMMdd-hhmm")
End If
wbk.Close
End If
Next
sFname = Dir()
Some fixes:
Sub CombineSheets()
Const sPath As String = "C:\Zoltan\TEST\"
Dim sFname As String
Dim wbk As Workbook
Dim wSht As Worksheet
Application.EnableEvents = False
Application.ScreenUpdating = False
'sPath already has a trailing \ - don't add another...
sFname = Dir(sPath & "*Mailing*.xl*", vbNormal)
Do Until sFname = ""
'Dir only gives you the filename - use full path below
Set wbk = Workbooks.Open(sPath & sFname)
For Each wSht In wbk.WorkSheets
If Not wSht.Name Like "*Mailing*" Then
wSht.Copy Before:=ThisWorkbook.Sheets(1)
End If
Next
'moved these lines out of the sheets loop
wbk.Close False
sFname = Dir()
Loop
ThisWorkbook.Save
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub

#REF! in formula after merging a workbook in Excel

I'm merging Excel workbooks into one "summary.xls" using a VBA macro. The macro is executed from another open workbook. This original workbook has some formulas containing links to "summary" (like ='C:\[Summary.xls]Cell'!E3). For the process of merging, the original workbook "summary.xls" is deleted and rewritten. After rewriting all the formulas with the original links to summary have #ref! written in it and are broken and can not be automatically updated (='C:\[Summary.xls]#REF'!E4). The following passage is the one causing the mistake:
Workbooks(Filename).Close (False) 'add False to close without saving
' Kill srcFile 'deletes the file
Filename = Dir()
Does somebody has a suggestion how to solve the problem?
Whole code is based on that suggestion:
Option Explicit
Function IsSheetEmpty(sht As Worksheet) As Boolean
IsSheetEmpty = Application.WorksheetFunction.CountA(sht.Cells) = 0
End Function
Sub GetSheets()
Dim Path, Filename As String
Dim Sheet As Worksheet
Dim newBook As Workbook
Dim appSheets As Integer
Dim srcFile As String
Dim dstFile As String
Application.ScreenUpdating = False 'go faster by not waiting for display
'--- create a new workbook with only one worksheet
dstFile = ActiveWorkbook.Path & "AllSheetsHere.xlsx"
If Dir(dstFile) <> "" Then
Kill dstFile 'delete the file if it already exists
End If
appSheets = Application.SheetsInNewWorkbook 'saves the default number of new sheets
Application.SheetsInNewWorkbook = 1 'force only one new sheet
Set newBook = Application.Workbooks.Add
newBook.SaveAs dstFile
Application.SheetsInNewWorkbook = appSheets 'restores the default number of new sheets
Path = "C:\Temp\"
Filename = Dir(Path & "*.xls?") 'add the ? to pick up *.xlsx and *.xlsm files
Do While Filename <> ""
srcFile = Path & Filename
Workbooks.Open Filename:=srcFile, ReadOnly:=True
For Each Sheet In ActiveWorkbook.Sheets
'--- potentially check for blank sheets, or only sheets
' with specific data on them
If Not IsSheetEmpty(Sheet) Then
Sheet.Copy After:=newBook.Sheets(1)
End If
Next Sheet
Workbooks(Filename).Close (False) 'add False to close without saving
Kill srcFile 'deletes the file
Filename = Dir()
Loop
'--- delete the original empty worksheet and save the book
newBook.Sheets(1).Delete
newBook.Save
newBook.Close
Application.ScreenUpdating = True 're-enable screen updates
End Sub
Internal sheet-to-sheet references within a workbook (Book1.xlsx) generally look like this:
=ABC!B23
But if you copy the worksheet with that reference to a new workbook, Excel will change it to an external reference back to the original workbook:
='[Book1.xlsx]ABC'!B23
There are several restrictions you'll have to place on references in your worksheets that you're copying into the single new workbook:
All sheet names in the destination workbook MUST be unique
Sheets named "ABC" in Book1 and "ABC" in Book2 would cause reference collisions in the destination workbook
One of the sheets must be renamed into a unique string
Sheet-to-sheet references that are completely internal to a workbook can be converted into similar references in the destination. References to external worksheets (in a different workbook) may be problematic and could require lots of additional logic to handle.
One option is to perform a wildcard search and replace on a worksheet after the Sheet.Copy is performed. The requirement here is that any sheet that is referenced must already be local to the new sheet in the destination book. (Otherwise, the "fixed-up" reference will still give you a #REF error.)
Sub test()
Dim area As Range
Dim farea As Range
'--- determines the entire used area of the worksheet
Set area = Range("A1").Resize(Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row, _
Cells.Find(What:="*", SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Column)
'--- replaces all external references to make them internal references
area.Replace What:="[*]", Replacement:=""
End Sub
The other option is much cleaner and a neat trick. When you're copying worksheets into a new workbook, if you copy ALL the sheets in a single action then Excel preserves the sheet-to-sheet references as internal (and doesn't replace each reference with a filename prefix) because it knows that the sheet references will be there in the new workbook. Here's that solution in your code:
Option Explicit
Function IsSheetEmpty(sht As Worksheet) As Boolean
IsSheetEmpty = Application.WorksheetFunction.CountA(sht.Cells) = 0
End Function
Sub GetSheets()
Dim i As Integer
Dim Path, Filename As String
Dim sh As Worksheet
Dim newBook As Workbook
Dim appSheets As Integer
Dim srcFile As String
Dim dstFile As String
Dim dstPath As String
Dim wasntAlreadyOpen As Boolean
Dim name As Variant
Dim allSheetNames As Dictionary 'check VBA Editor->Tools->References->Microsoft Scripting Runtime
Dim newSheetNames As Dictionary
Dim newNames() As String
Application.ScreenUpdating = False 'go faster by not waiting for display
'--- create a new workbook with only one worksheet
dstFile = "AllSheetsHere.xlsx"
dstPath = ActiveWorkbook.Path & "\" & dstFile
wasntAlreadyOpen = True
If Dir(dstPath) = "" Then
'--- the destination workbook does not (yet) exist, so create it
appSheets = Application.SheetsInNewWorkbook 'saves the default number of new sheets
Application.SheetsInNewWorkbook = 1 'force only one new sheet
Set newBook = Application.Workbooks.Add
newBook.SaveAs dstPath
Application.SheetsInNewWorkbook = appSheets 'restores the default number of new sheets
Else
'--- the destination workbook exists, so ...
On Error Resume Next
wasntAlreadyOpen = False
Set newBook = Workbooks(dstFile) 'connect if already open
If newBook Is Nothing Then
Set newBook = Workbooks.Open(dstPath) 'open if needed
wasntAlreadyOpen = True
End If
On Error GoTo 0
'--- make sure to delete any/all worksheets so we're only left
' with a single empty sheet named "Sheet1"
Application.DisplayAlerts = False 'we dont need to see the warning message
Do While newBook.Sheets.Count > 1
newBook.Sheets(newBook.Sheets.Count).Delete
Loop
newBook.Sheets(1).name = "Sheet1"
newBook.Sheets(1).Cells.ClearContents
newBook.Sheets(1).Cells.ClearFormats
Application.DisplayAlerts = True 'turn alerts back on
End If
'--- create the collections of sheet names...
' we need to make sure that all of the sheets added to the newBook have unique
' names so that any formula references between sheets will work properly
' LIMITATION: this assumes sheet-to-sheet references only exist internal to
' a single workbook. External references to sheets outside of the
' source workbook are unsupported in this fix-up
Set allSheetNames = New Dictionary
allSheetNames.Add "Sheet1", 1
Path = "C:\Temp\"
Filename = Dir(Path & "*.xls?") 'add the ? to pick up *.xlsx and *.xlsm files
Do While Filename <> ""
srcFile = Path & Filename
Workbooks.Open Filename:=srcFile, ReadOnly:=True
'--- first make sure all the sheet names are unique in the destination book
Set newSheetNames = New Dictionary
For Each sh In ActiveWorkbook.Sheets
If Not IsSheetEmpty(sh) Then
'--- loop until we get a unique name
i = 0
Do While allSheetNames.Exists(sh.name)
sh.name = sh.name & "_" & i 'rename until unique
i = i + 1
Loop
allSheetNames.Add sh.name, i
newSheetNames.Add sh.name, i
End If
Next sh
'--- we're going to copy ALL of the non-empty sheets to the new workbook with
' a single statement. the advantage of this method is that all sheet-to-sheet
' references are preserved between the sheets in the new workbook WITHOUT
' those references changed into external references
ReDim newNames(0 To newSheetNames.Count - 1)
i = 0
For Each name In newSheetNames.Keys
newNames(i) = name
i = i + 1
Next name
ActiveWorkbook.Sheets(newNames).Copy After:=newBook.Sheets(1)
Workbooks(Filename).Close (False) 'add False to close without saving
Kill srcFile 'deletes the file
'--- get the next file that matches
Filename = Dir()
Loop
'--- delete the original empty worksheet and save the book
If newBook.Sheets.Count > 1 Then
newBook.Sheets(1).Delete
End If
newBook.Save
'--- leave it open if it was already open when we started
If wasntAlreadyOpen Then
newBook.Close
End If
Application.ScreenUpdating = True 're-enable screen updates
End Sub
If you still have reference in your workbook to the cells being referenced (and from your example, you do), and if all of your #REF! errors used to point to a single sheet, there is an easy fix.
CTRL+H brings up the REPLACE function.
Simply enter #REF! in the "find" box, and Sheet1 in the "replace" box, and all references will now point to sheet1 in the same summary.xls workbook.
I've added a further workbook containig the referencins formulas. This one is closed during the whole procedure of deleting and summarizing the worksheets. The new workbook opens after this, therefore the referencing mistake is avoided.

Resources