+ Reply to Thread
Results 1 to 12 of 12

Macro to merge 2 sheets to one and keep header row

Hybrid View

  1. #1
    Registered User
    Join Date
    01-05-2012
    Location
    toronto, canada
    MS-Off Ver
    Excel 2003
    Posts
    13

    Macro to merge 2 sheets to one and keep header row

    I have sheets named PO_1 and PO_2 with the same number of columns and header names. The number of rows changes each day from a query refresh.
    I want to copy the rows (A2:F2 down to last row) from each of these sheets to a sheet named "Totals" which already has the header row

    I have read through many posts on this problem and tried about 10 different macros but I was not able to get any of them to work for me so I had to ask the question.

  2. #2
    Forum Contributor
    Join Date
    10-12-2012
    Location
    Bournemouth
    MS-Off Ver
    Excel 2010 / Excel 2007
    Posts
    126

    Re: Macro to merge 2 sheets to one and keep header row

    Hi, I'd say you'd need to try something like:

    Sub cheeze83()
    sheets("Totals").Visible = True 'jut in case the totals sheet is usually hidden
    sheets("PO_1").select
    range("A2:F2").select
    range(selection, selection.end(xldown)).select
    selection.copy
    sheets("Totals").select
    range("a2").select
    selection.end(xldown).select 'this will go to the last row with data to ensure old data isn't overwritten
    activecell.offset(1,0).select 'will go one cell down, to the first blank cell
    activesheet.paste
    sheets("PO_2").select
    range("A2:F2").select
    range(selection, selection.end(xldown)).select
    selection.copy
    Sheets("Totals").select
    range("A2").select
    selection.end(xldown).select
    activecell.offset(1,0).select
    activesheet.paste
    
    End Sub
    If my answer helped pls click the star =)

  3. #3
    Registered User
    Join Date
    01-05-2012
    Location
    toronto, canada
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Macro to merge 2 sheets to one and keep header row

    I am getting an error on this line
    ActiveCell.Offset(1, 0).Select 'will go one cell down, to the first blank cell

    it is going down to A65536 on Totals sheet

  4. #4
    Forum Contributor
    Join Date
    10-12-2012
    Location
    Bournemouth
    MS-Off Ver
    Excel 2010 / Excel 2007
    Posts
    126

    Re: Macro to merge 2 sheets to one and keep header row

    or saying that you could just change it around so it works the other way which would be like this

    Sub cheeze83()
    sheets("Totals").Visible = True 'jut in case the totals sheet is usually hidden
    sheets("PO_1").select
    range("A2:F2").select
    range(selection, selection.end(xldown)).select
    selection.copy
    sheets("Totals").select
    range("a65536").select
    selection.end(xlup).select 'this will go to the last row with data to ensure old data isn't overwritten
    activecell.offset(1,0).select 'will go one cell down, to the first blank cell
    activesheet.paste
    sheets("PO_2").select
    range("A2:F2").select
    range(selection, selection.end(xldown)).select
    selection.copy
    Sheets("Totals").select
    range("A65536").select
    selection.end(xlup).select
    activecell.offset(1,0).select
    activesheet.paste
    
    End Sub

  5. #5
    Forum Contributor
    Join Date
    10-12-2012
    Location
    Bournemouth
    MS-Off Ver
    Excel 2010 / Excel 2007
    Posts
    126

    Re: Macro to merge 2 sheets to one and keep header row

    Ah, I'm assuming there is currently no data in the totals spreadsheet?

    If this is the case take out the first

    selection.end(xldown).select 'this will go to the last row with data to ensure old data isn't overwritten
    activecell.offset(1,0).select 'will go one cell down, to the first blank cell
    so you are left with

    Sub cheeze83()
    sheets("Totals").Visible = True 'jut in case the totals sheet is usually hidden
    sheets("PO_1").select
    range("A2:F2").select
    range(selection, selection.end(xldown)).select
    selection.copy
    sheets("Totals").select
    range("a2").select
    activesheet.paste
    sheets("PO_2").select
    range("A2:F2").select
    range(selection, selection.end(xldown)).select
    selection.copy
    Sheets("Totals").select
    range("A2").select
    selection.end(xldown).select
    activecell.offset(1,0).select
    activesheet.paste
    
    End Sub

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Macro to merge 2 sheets to one and keep header row

    try
    Sub test()
        Dim e
        For Each e In Array("PO_1", "PO_2")
            Sheets(e).Range("a2").CurrentRegion.Offset(1).Copy _
            Sheets("totals").Range("a" & Rows.Count).End(xlUp)(2)
        Next
    End Sub

  7. #7
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Macro to merge 2 sheets to one and keep header row

    Hi Carcel,

    Based on this recent post by me for A.Patel, try this:

    Option Explicit
    Sub Macro1()
    
        'Written by Trebor76
        'Visit my website www.excelguru.net.au
        
        'Starting at Row 2, copy all records from the 'PO_1' and 'PO_2' tabs for Col.'s A to F to next available row in Col. A of the 'Totals' tab.
        
        'http://www.excelforum.com/excel-programming-vba-macros/964890-macro-to-merge-2-sheets-to-one-and-keep-header-row.html
    
        Dim wstMySheet As Worksheet
        Dim rngCell As Range
        Dim lngEndRow As Long, _
            lngPasteRow As Long
        
        Application.ScreenUpdating = False
        
        For Each wstMySheet In ThisWorkbook.Sheets
            If wstMySheet.Name <> "Totals" Then
                lngEndRow = wstMySheet.Cells(Rows.Count, "A").End(xlUp).Row
                lngPasteRow = Sheets("Totals").Cells(Rows.Count, "A").End(xlUp).Row + 1
                Range(wstMySheet.Cells(2, "A"), wstMySheet.Cells(lngEndRow, "F")).Copy Destination:=Sheets("Totals").Cells(lngPasteRow, "A")
            End If
        Next wstMySheet
        
        Application.ScreenUpdating = True
        
        MsgBox "All applicable rows have now been copied to the ""Totals"" tab.", vbInformation, "Excel Guru"
    
    End Sub
    HTH

    Robert
    Last edited by Trebor76; 10-30-2013 at 07:59 AM. Reason: Fix code tags
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  8. #8
    Registered User
    Join Date
    01-05-2012
    Location
    toronto, canada
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Macro to merge 2 sheets to one and keep header row

    Both of those solutions work but I forgot to add that I needed special paste of values only

  9. #9
    Forum Contributor
    Join Date
    10-12-2012
    Location
    Bournemouth
    MS-Off Ver
    Excel 2010 / Excel 2007
    Posts
    126

    Re: Macro to merge 2 sheets to one and keep header row

    For my code - just change

    activesheet.paste
    to

    activesheet.pastespecial paste:=xlvalues

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Macro to merge 2 sheets to one and keep header row

    Sub test()
        Dim e
        For Each e In Array("PO_1", "PO_2")
            Sheets(e).Range("a2").CurrentRegion.Offset(1).Copy
            Sheets("totals").Range("a" & Rows.Count).End(xlUp)(2).PasteSpecial xlPasteValues
        Next
    End Sub

  11. #11
    Registered User
    Join Date
    01-05-2012
    Location
    toronto, canada
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Macro to merge 2 sheets to one and keep header row

    Jindon, that works.

    cheeze83 I get an error with that line
    activesheet.pastespecial paste:=xlvalues

    Thanks

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Macro to merge 2 sheets to one and keep header row

    Quote Originally Posted by Capcel View Post
    Jindon, that works.
    Good.
    Quote Originally Posted by Capcel View Post
    cheeze83 I get an error with that line
    activesheet.pastespecial paste:=xlvalues
    Sheet object doesn't have PasteSpecial method.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Macro - Merge 16 sheets, first row problem on empty sheet. Please help.
    By lagiosman in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-11-2013, 06:45 AM
  2. [SOLVED] Macro to Compare/Merge 2 Excel Sheets
    By Anthony123 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-30-2013, 10:16 AM
  3. [SOLVED] Macro to Merge Sequential Numbered Sheets
    By lesoies in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-06-2012, 08:12 AM
  4. Macro code to pull data from different sheets based on the header of the main sheet
    By Shanthuday in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-21-2012, 05:00 AM
  5. Replies: 2
    Last Post: 06-23-2006, 09:15 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1