Link to home
Start Free TrialLog in
Avatar of Ramona
RamonaFlag for United States of America

asked on

How to merge multiple spreadsheets in Excel 2003

Hi Experts. I have collected storage performance data for 30 MS Exchange servers. The higher ups want to see it in a spreadsheet. How do I merge these documents into a single spreadsheet? Thank you!
Avatar of redmondb
redmondb
Flag of Afghanistan image

Hi, Rhiaanon44.

Please see the code below. A few notes...
(1) Set the XHEADER_ROWS constant to the no. of rows in the header (so it isn't duplicated from the second and subsequent sheets). You can set this to 0 if you want all rows from all sheets.
(2) The macro unprotects, unfilters, unhides rows and columns the two sheets.
(3) Because formulas may cause problems in the new "Merge" sheet, the macro pastes values only. Obviously it can be changed if you want formatting also or formulas instead.
(4) The macro doesn't bother checking if "Merge" already exists - instead it fails ungracefully.
(5) The results are slightly untidy if the second (or subsequent) sheet only has the header rows (i.e. no data).
Option Explicit

Const XHEADER_ROWS = 1

Sub Merge_Multiple_Sheets_Paste_Values()

Dim xNewSheet As Worksheet
Dim xOldSheet As Worksheet
Dim xSheet As Worksheet
Dim xResponse As Long
Dim xhold As String
Dim xLast_Row As Long
Dim xStart_Row As Long
Dim xFirst As Boolean

With Application
    .EnableEvents = False
    .ScreenUpdating = False
    .Calculation = xlManual
End With

    Set xNewSheet = Sheets.Add
    xNewSheet.Name = "Merge"
    xFirst = True
    
    For Each xOldSheet In ActiveWorkbook.Sheets '(Array("Sheet1", "Sheet2"))
    
        If Not xOldSheet.Name = "Merge" Then
            
            If xOldSheet.ProtectContents Then xOldSheet.Unprotect ' If there's a password then add it as a string here.
            If xOldSheet.FilterMode Then xOldSheet.ShowAllData
            
            xOldSheet.Cells.EntireColumn.Hidden = False
            xOldSheet.Cells.EntireRow.Hidden = False
            
            xLast_Row = Sheets("Merge").Range("A1").SpecialCells(xlLastCell).ROW
            If xFirst Then
                xStart_Row = 1
                xFirst = False
            Else
                '''If xLast_Row = XHEADER_ROWS Then Exit For
                xStart_Row = XHEADER_ROWS + 1
                xLast_Row = xLast_Row + 1
            End If
            
            xOldSheet.Range("A" & xStart_Row & ":" & xOldSheet.Range("A1").SpecialCells(xlLastCell).Address).Copy
            Sheets("Merge").Cells(xLast_Row, 1).PasteSpecial (xlPasteValues)
        
        End If
        
    Next
    
    Sheets("Merge").Range("A1").Activate
    
With Application
    .EnableEvents = True
    .ScreenUpdating = True
    .Calculation = xlAutomatic
End With

End Sub

Open in new window

Regards,
Brian.
Rhiaanon44,

Ehm, I should probably have said that I am assuming the data is already in 30 sheets in the workbook. If any of this is incorrect, please let me know the details!

Regards,
Brian.
Avatar of Ramona

ASKER

Hi Redmonb.All 30 are in a spreadsheet. However, I should have said I am novice with excel. This work was given to me because I am the new guy.  =)  
Rhiaanon44,

We were all there once, but at least you've found your way here!

What we're going to do isn't going to be too complicated - as long as I explain properly, so please bear with me. :)

OK, some questions,...
(1) Which version of Excel are you using?
(2) If the file isn't too big, can you post it here? (If the data is confidential (or you're just not sure) then the correct answer is "No".) If not, can you tell me
(a) what kind of file it is,
(b) on average, roughly how many rows per sheet,
(c)  Are the headers for each sheet identical?
(3) Have you ever looked at a macro within Excel?

Thanks,
Brian.
Avatar of Ramona

ASKER

There are 37 server names but I just put several bogus ones there. Also, there are 6 columns more of these kind of data. All 30 sheets have the same data requested for the 37 servers.                                                            
 Excel.xlsx
ASKER CERTIFIED SOLUTION
Avatar of redmondb
redmondb
Flag of Afghanistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
John?! Sorry, Rhiaanon44, I mixed up two posts. Definitely time for bed!
Avatar of Ramona

ASKER

Thanks Redmonb. I will be back on in the AM. I am looking at your work. Awesome! Thanks!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ramona

ASKER

Thank you both! I cannot thank you enough!!!
Thanks, Rhiaanon44. Good suggestion Rob.