+ Reply to Thread
Results 1 to 4 of 4

Thread: Copying data from multiple worksheets to a master worksheet

  1. #1
    Registered User
    Join Date
    06-17-2009
    Location
    Connecticut
    MS-Off Ver
    Excel 2007
    Posts
    7

    Question Copying data from multiple worksheets to a master worksheet

    Hi, I'm a beginner at Excel, so please bear with me.

    I have a workbook with multiple worksheets. Each worksheet is a set o data from a certain year. I want to create a macro that automatically copies the data from these worksheets into a master worksheet that can easily be used to make a pivot table. How can I do this? (I included a sample workbook in the attachments.)
    Attached Files Attached Files
    Last edited by Michael.H.; 06-23-2009 at 08:34 PM.

  2. #2
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Copying data from multiple worksheets to a master worksheet

    If every sheet has the exact same column headers, all the data is already in the correct place and you just want "Summary" report created, then this basic macro does that:
    Option Explicit
    
    Public Function SheetExists(SName As String, Optional ByVal Wb As Workbook) As Boolean
    'Chip Pearson
    On Error Resume Next
        If Wb Is Nothing Then Set Wb = ThisWorkbook
            SheetExists = CBool(Len(Wb.Sheets(SName).Name))
    End Function
    
    Sub SummaryReport()
    'JBeaucaire (6/21/2009)
    'Copy rows of data from multiple sheets to one matching one ID #
    Dim ws As Worksheet, LR As Long, NR As Long
    
    'Setup
        If SheetExists("Report") Then
            Sheets("Report").Range("A2:Z" & Rows.Count).ClearContents
        Else
            Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Report"
            Sheets(1).Rows(1).EntireRow.Copy Sheets("Report").Range("A1")
        End If
    
        NR = 2
    
    'Copy data to Report
        For Each ws In Worksheets
            If ws.Name <> "Report" Then
                ws.Activate
                LR = Range("A" & Rows.Count).End(xlUp).Row
                Range("A2:Z" & LR).Copy Sheets("Report").Range("A" & NR)
                NR = NR + LR - 1
            End If
        Next ws
    
    End Sub
    Run the SummaryReport macro (press Alt-F8)

    If you run it successively, it erases the existing report and creates a new one.

    If you don't want the old report erased but want new data appended to the bottom of the existing report, comment out or erase the "optional" line of code in the macro.
    Attached Files Attached Files
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    06-17-2009
    Location
    Connecticut
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Copying data from multiple worksheets to a master worksheet

    Thanks a million! This is great!

  4. #4
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Copying data from multiple worksheets to a master worksheet

    If that takes care of your need, be sure to EDIT your original post, click Go Advanced and mark the PREFIX box [SOLVED].


    (Also, use the blue "scales" icon in our posts to leave Reputation Feedback, it is appreciated)
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0