Merge sheets of a workbook macro

doriannjeshi

Board Regular
Joined
Apr 5, 2015
Messages
244
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have an .xls file that is exported and depending on the data it might have 1 sheet to 5 or more . If it was an .xslx file it would be in one sheet but .xls file the data gets divided in 65536 rows per sheet
Is it possible to have a macro that copies all data from all the sheets and paste it in a single sheet on a new workbook .xlsx
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Try this macro:

VBA Code:
Public Sub Merge_xls_Sheets()

    Dim xlsFile As Variant
    Dim xlsWb As Workbook
    Dim xlsxWb As Workbook
    Dim ws As Worksheet
    Dim destCell As Range
    
    xlsFile = Application.GetOpenFilename(Title:="Select .xls file", FileFilter:="Excel 97-2003 workbook (*.xls), *.xls")
    If xlsFile = False Then Exit Sub
    
    Set xlsxWb = Workbooks.Add(xlWBATWorksheet)
    Set destCell = xlsxWb.Worksheets(1).Range("A1")
    
    Set xlsWb = Workbooks.Open(xlsFile)
    For Each ws In xlsWb.Worksheets
        ws.UsedRange.Copy destCell
        With destCell.Worksheet
            Set destCell = .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
        End With
    Next
    xlsWb.Close False
    
    Application.DisplayAlerts = False
    xlsxWb.Close True, Replace(xlsFile, ".xls", ".xlsx")
    Application.DisplayAlerts = True
    
End Sub
 
Upvote 0
I get this
1730494903682.png


1730494915465.png
 
Upvote 0
Try this macro instead:
VBA Code:
Public Sub Merge_xls_Sheets()

    Dim xlsFile As Variant
    Dim xlsWb As Workbook
    Dim xlsxWb As Workbook
    Dim ws As Worksheet
    Dim destCell As Range
    
    xlsFile = Application.GetOpenFilename(Title:="Select .xls file", FileFilter:="Excel 97-2003 workbook (*.xls), *.xls")
    If xlsFile = False Then Exit Sub
    
    Set xlsxWb = Workbooks.Add(xlWBATWorksheet)
    Set destCell = xlsxWb.Worksheets(1).Range("A1")
    
    Set xlsWb = Workbooks.Open(xlsFile)
    For Each ws In xlsWb.Worksheets
        ws.UsedRange.Copy
        destCell.PasteSpecial
        With destCell.Worksheet
            Set destCell = .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
        End With
    Next   
    Application.CutCopyMode = False
    xlsWb.Close False
    
    Application.DisplayAlerts = False
    xlsxWb.Close True, Replace(xlsFile, ".xls", ".xlsx")
    Application.DisplayAlerts = True
    
End Sub
 
Upvote 0
Using John's suggestion, if you change this part

Code:
    For Each ws In xlsWb.Worksheets
        ws.UsedRange.Copy destCell
        With destCell.Worksheet
            Set destCell = .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
        End With
    Next

to this

Code:
    For Each ws In xlsWb.Worksheets
        destCell.Resize(ws.UsedRange.Rows.Count, ws.UsedRange.Columns.Count).Value = ws.UsedRange.Value
        Set destCell = xlsxWb.Worksheets(1).Cells(.Rows.Count, "A").End(xlUp).Offset(1)
    Next ws

Does it work then?
 
Upvote 0
1730568649043.png


Using John's suggestion, if you change this part

Code:
    For Each ws In xlsWb.Worksheets
        ws.UsedRange.Copy destCell
        With destCell.Worksheet
            Set destCell = .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
        End With
    Next

to this

Code:
    For Each ws In xlsWb.Worksheets
        destCell.Resize(ws.UsedRange.Rows.Count, ws.UsedRange.Columns.Count).Value = ws.UsedRange.Value
        Set destCell = xlsxWb.Worksheets(1).Cells(.Rows.Count, "A").End(xlUp).Offset(1)
    Next ws

Does it work then?
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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