Combine Sheets in Excel

Nikhil J Shriyan

New Member
Joined
May 27, 2012
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Hello ,

I had this macro to combine Sheets in excel together which used to work in Excel 2007 but this is not working in excel 365. Does anybody Help me.

I want to Merge Excel sheets from the same Excel Together in One Sheet.


VBA Code:
Sub CombineSheets()
'
' Macro3 Macro
'

'
    Sheets.Add(Before:=Sheets(1)).Name = "Master"
    Sheets("Master").Activate
   
    'Sub SheetNames()
Columns(1).Insert
For i = 1 To Sheets.Count
Cells(i, 1) = Sheets(i).Name
Next i
'End Sub

ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
   
    ActiveWindow.FreezePanes = True

If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If

'1
    Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Master").Select

    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
    Selection.End(xlToLeft).Select
If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If
       ActiveWindow.SelectedSheets.Delete
      
      
  '2
  Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Master").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
    Selection.End(xlToLeft).Select
If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If
       ActiveWindow.SelectedSheets.Delete
      
 
  '3
    Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Master").Select

    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
    Selection.End(xlToLeft).Select
If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If
       ActiveWindow.SelectedSheets.Delete
      
  '4
  Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Master").Select

    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
    Selection.End(xlToLeft).Select
If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If
       ActiveWindow.SelectedSheets.Delete
      
 
  '5
    Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Master").Select

    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
    Selection.End(xlToLeft).Select
If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If
       ActiveWindow.SelectedSheets.Delete
      
      
  '6
  Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Master").Select

    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
    Selection.End(xlToLeft).Select
If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If
       ActiveWindow.SelectedSheets.Delete
      
      
  '6
  Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Master").Select
 
    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
    Selection.End(xlToLeft).Select
If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If
       ActiveWindow.SelectedSheets.Delete
      
      
      
  '7
  Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Master").Select

    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
    Selection.End(xlToLeft).Select
If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If
       ActiveWindow.SelectedSheets.Delete
      
      
  '8
  Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Master").Select

    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
    Selection.End(xlToLeft).Select
If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If
       ActiveWindow.SelectedSheets.Delete
      
      
  '9
  Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Master").Select

    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
    Selection.End(xlToLeft).Select
If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If
       ActiveWindow.SelectedSheets.Delete
      
      
  '10
    Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Master").Select

    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
    Selection.End(xlToLeft).Select
If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If
       ActiveWindow.SelectedSheets.Delete
      
      
  '11
  Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Master").Select

    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
    Selection.End(xlToLeft).Select
If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If
       ActiveWindow.SelectedSheets.Delete
      
      
  '12
  Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Master").Select

    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
    Selection.End(xlToLeft).Select
If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If
       ActiveWindow.SelectedSheets.Delete
      
      
  '13
  Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Master").Select

    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
    Selection.End(xlToLeft).Select
If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If
       ActiveWindow.SelectedSheets.Delete
      
      
  '14
  Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Master").Select

    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
    Selection.End(xlToLeft).Select
If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If
       ActiveWindow.SelectedSheets.Delete
      
      
      
  '15
  Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Master").Select

    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
    Selection.End(xlToLeft).Select
If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If
       ActiveWindow.SelectedSheets.Delete
      
      
  '16
  Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Master").Select

    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
    Selection.End(xlToLeft).Select
If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If
       ActiveWindow.SelectedSheets.Delete
      
      
  '17
  Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Master").Select

    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
    Selection.End(xlToLeft).Select
If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If
       ActiveWindow.SelectedSheets.Delete
      
  '18
  Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Master").Select

    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
    Selection.End(xlToLeft).Select
If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If
       ActiveWindow.SelectedSheets.Delete
      
      
  '19
   Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Master").Select

    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
    Selection.End(xlToLeft).Select
If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If
       ActiveWindow.SelectedSheets.Delete
      
      
'20
    Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Master").Select

    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
    Selection.End(xlToLeft).Select
If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If
       ActiveWindow.SelectedSheets.Delete
      
      
      
  '21
  Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Master").Select

    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
    Selection.End(xlToLeft).Select
If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If
       ActiveWindow.SelectedSheets.Delete
      
      
         '22
  Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Master").Select

    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
    Selection.End(xlToLeft).Select
If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If
       ActiveWindow.SelectedSheets.Delete
      
      
         '23
  Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Master").Select

    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
    Selection.End(xlToLeft).Select
If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If
       ActiveWindow.SelectedSheets.Delete
      
         '24
  Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Master").Select

    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
    Selection.End(xlToLeft).Select
If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If
       ActiveWindow.SelectedSheets.Delete
      
         '25
  Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Master").Select

    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
    Selection.End(xlToLeft).Select
If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If
       ActiveWindow.SelectedSheets.Delete
      
      
         '26
  Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Master").Select

    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
    Selection.End(xlToLeft).Select
If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If
       ActiveWindow.SelectedSheets.Delete
      
         '27
  Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Master").Select

    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveCell.SpecialCells(xlLastCell).Select
    ActiveCell.Offset(1, 0).Select
    Selection.End(xlToLeft).Select
If ActiveSheet.Index = Worksheets.Count Then
    Worksheets(1).Activate
Else
    ActiveSheet.Next.Activate
End If
       ActiveWindow.SelectedSheets.Delete
      
   
End Sub
 
Last edited by a moderator:

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
There is nothing in the macro that would cause it to fail in MS 365.
I have converted your code from a Recorded Macro which repeats the same code over and over again to a version using a loop.
I have tried to stay close to what you had to help you follow the conversion process.

This should make it easier to trouble shoot if it is still not doing what you are expecting it to.
If it errors out show us the error message and the line highlighted in the code when you press on debug.
If the results aren't what you expected, show us the result and the expected result and ideally one of the sheets it is copying from.

*** warning this macro (as did the original) deletes sheets, so make sure you are working on a COPY of your workbook ***

VBA Code:
Sub CombineSheetsModified()

    Application.ScreenUpdating = False

    Dim shtMstr As Worksheet, shtToCopy As Worksheet
    Dim nextRowMstr As Long

    Sheets.Add(Before:=Sheets(1)).Name = "Master"
    Set shtMstr = Worksheets("Master")
  
    ' List Sheet Names
    For i = 1 To Sheets.Count
        shtMstr.Cells(i, 1) = Sheets(i).Name
    Next i
   
    shtMstr.Cells.SpecialCells(xlLastCell).Offset(1, 0).Select
    ActiveWindow.FreezePanes = True

    ' Copy Data from Other Sheets
    For i = 2 To Sheets.Count
        Set shtToCopy = Worksheets(i)
        nextRowMstr = shtMstr.Cells.SpecialCells(xlLastCell).Offset(1, 0).Row
        shtToCopy.Range("A1", shtToCopy.Cells.SpecialCells(xlLastCell)).Copy _
            Destination:=shtMstr.Range("A" & nextRowMstr)
    Next i
   
    ' Delete Other Sheets
    Application.DisplayAlerts = False
    For i = Sheets.Count To 2 Step -1
        Worksheets(i).Delete
    Next i
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

End Sub
 
Last edited:
Upvote 0
There is nothing in the macro that would cause it to fail in MS 365.
I have converted your code from a Recorded Macro which repeats the same code over and over again to a version using a loop.
I have tried to stay close to what you had to help you follow the conversion process.

This should make it easier to trouble shoot if it is still not doing what you are expecting it to.
If it errors out show us the error message and the line highlighted in the code when you press on debug.
If the results aren't what you expected, show us the result and the expected result and ideally one of the sheets it is copying from.

*** warning this macro (as did the original) deletes sheets, so make sure you are working on a COPY of your workbook ***

VBA Code:
Sub CombineSheetsModified()

    Application.ScreenUpdating = False

    Dim shtMstr As Worksheet, shtToCopy As Worksheet
    Dim nextRowMstr As Long

    Sheets.Add(Before:=Sheets(1)).Name = "Master"
    Set shtMstr = Worksheets("Master")
 
    ' List Sheet Names
    For i = 1 To Sheets.Count
        shtMstr.Cells(i, 1) = Sheets(i).Name
    Next i
  
    shtMstr.Cells.SpecialCells(xlLastCell).Offset(1, 0).Select
    ActiveWindow.FreezePanes = True

    ' Copy Data from Other Sheets
    For i = 2 To Sheets.Count
        Set shtToCopy = Worksheets(i)
        nextRowMstr = shtMstr.Cells.SpecialCells(xlLastCell).Offset(1, 0).Row
        shtToCopy.Range("A1", shtToCopy.Cells.SpecialCells(xlLastCell)).Copy _
            Destination:=shtMstr.Range("A" & nextRowMstr)
    Next i
  
    ' Delete Other Sheets
    Application.DisplayAlerts = False
    For i = Sheets.Count To 2 Step -1
        Worksheets(i).Delete
    Next i
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

End Sub
Hello

This Macro when I run Close's the Excel and Opens New Excel, But Doesn't combine anything
 
Upvote 0
There is nothing in the code that opens and closes Excel.
Where have you put the code ?
Is it in the workbook that you want to get the sheets from to combine ?
Do you have it in a standard module (not a worksheet or workbook module) ?
How are you kicking it of to run ?
Is your workbook or worksheets protected ?
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,126
Members
452,381
Latest member
Nova88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top