Compiling data from different worksheets.

xlbaby

New Member
Joined
Nov 5, 2012
Messages
39
Hey guys,
I have a situation where I have to compile a lot of data from different columns from different worksheets.

I have the Masterfile where in all the compiled data has to flow. The other two worksheets (2 is for an example, it may go up to 5 worksheets), worksheet 1 and worksheet 2 are the files from where this data has to be pulled from. The only thing common in all these 3 files are the headings i.e. the headings reported on the master file is same as the headings in WS1 and WS2.

So I want the information under respective headings from WS1 and WS2 to flow into the same headings under Masterfile.

The trick here is that the details to be compiled in Masterfile are situated either in WS1 or WS2. So I need a macro to do this job.

Please help me on this. I have copied below the format in which I have the masterfile. I can give more explanation if it will be of any help.

IDUnique IDNameSurnameCountryGrand Total of Net Company Payments - carryover to 2016Federal Tax WithheldPaid by CompanyPaid by IndividualFederal Balance due or (Refund) - excluding P&IPaid by CompanyPaid by IndividualInterest & PenaltyPaid/Reimbursed by CompanyPaid by IndividualOther Settlement Amount Paid by CompanyTOTAL FED TAXTotal Amount Paid by CompanyLess: Current Period G/U Provided by CompanyNet Company PaymentSTATEState Tax WithheldPaid by CompanyPaid by Individual
WS1WS1/WS2WS1WS1WS1WS1WS2WS3WS3WS1WS3Ws3WS1WS1WS3

<tbody>
</tbody>




Really appreciate your help.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Try this:
Code:
Sub Copy_SheetsTwo_To_Last()
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Dim Lastrowa As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row + 1
    For i = 2 To Sheets.Count
        Lastrowa = Sheets(i).Cells(Rows.Count, "A").End(xlUp).Row
        Lastrow = Sheets("Master").Cells(Rows.Count, "A").End(xlUp).Row + 1
        Sheets(i).Range("A2:Z" & Lastrowa).Copy Destination:=Sheets("Master").Range("A" & Lastrow)
    Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thank you for this code.

I am a newbie in excel, can you please help me understand how this will work?
 
Upvote 0
See comments.

Code:
' this is your sub procedure
' you can either directly run a sub procedure
' from within the VBA editor (ALT + F11)
' or you can assign it to a button (there are other ways as well).
Sub Copy_SheetsTwo_To_Last()


    ' disabling screen updating will speed up your macro code.
    ' it tells excel not to update your screen with with information
    ' every time it makes a calculation. even though you cant see
    ' your macro being executed, it does, and faster.
    Application.ScreenUpdating = False


    ' delcare our variable types
    Dim i As Long
    Dim Lastrow As Long
    Dim Lastrowa As Long


    ' this will return the last row of our active worksheet
        ' Worksheet.Cells Property:    https://msdn.microsoft.com/en-us/library/office/ff194567.aspx
        '           Count Function:    https://msdn.microsoft.com/en-us/library/office/ff193349.aspx
    Lastrow = Cells(Rows.Count, "A").End(xlUp).Row + 1
    
    ' for each sheet (Sheets.Count),
    ' starting with sheet 2 (so as to skip the master sheet)
    For i = 2 To Sheets.Count


        ' return the last row of the selected worksheet (Sheets(i))
        ' where i is equal to the worksheet number.
        Lastrowa = Sheets(i).Cells(Rows.Count, "A").End(xlUp).Row


        ' return the last row of the "Master" worksheet
        Lastrow = Sheets("Master").Cells(Rows.Count, "A").End(xlUp).Row + 1


        ' for each worksheet, other then master, copy the range from cell A2,
        ' to the last row in Z; then copy that data beginning at the last
        ' used row of "Master" worksheet
        Sheets(i).Range("A2:Z" & Lastrowa).Copy Destination:=Sheets("Master").Range("A" & Lastrow)
    Next


    ' make sure we enable screen updating again
    ' before we end our macro so as to restore expected
    ' behavior while using excel.
    Application.ScreenUpdating = True


End Sub
 
Last edited:
Upvote 0
Thanks for providing that detailed explanation Krayons.
See comments.

Code:
' this is your sub procedure
' you can either directly run a sub procedure
' from within the VBA editor (ALT + F11)
' or you can assign it to a button (there are other ways as well).
Sub Copy_SheetsTwo_To_Last()


    ' disabling screen updating will speed up your macro code.
    ' it tells excel not to update your screen with with information
    ' every time it makes a calculation. even though you cant see
    ' your macro being executed, it does, and faster.
    Application.ScreenUpdating = False


    ' delcare our variable types
    Dim i As Long
    Dim Lastrow As Long
    Dim Lastrowa As Long


    ' this will return the last row of our active worksheet
        ' Worksheet.Cells Property:    https://msdn.microsoft.com/en-us/library/office/ff194567.aspx
        '           Count Function:    https://msdn.microsoft.com/en-us/library/office/ff193349.aspx
    Lastrow = Cells(Rows.Count, "A").End(xlUp).Row + 1
    
    ' for each sheet (Sheets.Count),
    ' starting with sheet 2 (so as to skip the master sheet)
    For i = 2 To Sheets.Count


        ' return the last row of the selected worksheet (Sheets(i))
        ' where i is equal to the worksheet number.
        Lastrowa = Sheets(i).Cells(Rows.Count, "A").End(xlUp).Row


        ' return the last row of the "Master" worksheet
        Lastrow = Sheets("Master").Cells(Rows.Count, "A").End(xlUp).Row + 1


        ' for each worksheet, other then master, copy the range from cell A2,
        ' to the last row in Z; then copy that data beginning at the last
        ' used row of "Master" worksheet
        Sheets(i).Range("A2:Z" & Lastrowa).Copy Destination:=Sheets("Master").Range("A" & Lastrow)
    Next


    ' make sure we enable screen updating again
    ' before we end our macro so as to restore expected
    ' behavior while using excel.
    Application.ScreenUpdating = True


End Sub
 
Upvote 0
Thank you my answer is this for the code and thank you krayons for the explanation.

I ran the code and I noticed an issue. The code pulls the information only from sheet 1 and not from other sheets within the masterfile.

So as I mentioned in my question, the data needs to be compiled from 3-4 different worksheets which is in different tabs within masterfile. To further clarify my question, I have put my how the details needs to be pulled.

Masterfile ColumnSheet 1 ColumnSheet 2 ColumnSheet 2 Column
AE
BF
CD
DG
EZ
FCC

<colgroup><col><col span="3"></colgroup><tbody>
</tbody>

The only thing common between the masterfile and other sheets are the headings. So it would be great if you can help me with the macro where it will look for the same headings under sheets 1, sheet 2 and sheet 3 and compile it under the same heading within masterfile. I hope I am clear now. Please let me know if any further explanation is required.

Thank you a lot for helping me on this.
 
Upvote 0
My script pulls all the data from sheet (2) to the last sheet in the workbook.
Are we dealing with all sheets. Meaning copy sheets 2 to last sheet into sheet (1) which is Master sheet?

Now that you have provided column Identifiers like "A "E" etc. I may be able to write a new script.
Attempting to do this with only column headers is much more difficult.
Is sheet named "Master" sheet number (1) meaning it is in far left position on tab bar.
 
Upvote 0
Yes. The sheet named master is in far left position on tab bar.

When I ran the macro, I understood that your sheet pulls the data from sheet 2 to the last sheet in the workbook, which is why I thought I will clarify it a bit more. I hope you understood what I wanted.

Thanks again!!
 
Upvote 0
Your second post saying this confused me:

"I ran the code and I noticed an issue. The code pulls the information only from sheet 1 and not from other sheets within the Masterfile."
 
Upvote 0
I meant sheet 2..master sheet is sheet 1. I apologise for confusing you.. hope it is clear now.
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

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