VBA Script to automatically copy & paste data from multiple worksheets to a master sheet

steve2115

Board Regular
Joined
Mar 17, 2014
Messages
82
Looking for a vba script to automatically copy & paste data from multiple worksheets to a master sheet named Returns. The data that I need to copy resides in columns A through N on multiples sheets. These sheet names are Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec. However I do not want to copy the header row for each so Row1 on each of these sheets should be excluded. Also, I want to exclude all row data that has "GR for acc.assgt rev"
in column C of all sheets.


Any help would be appreciated.

Thanks in Advance
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Perhaps something like this might work out for you. YOu did not state which column might contain the "GR for acc.assgt rev" entry, but I assumed that it might be column A using this code. I moved everything over to the Returns Sheet and then used an autofilter feature to remove those entries. I further assumed that all sheets were in the active workbook and any sheet not named returns would be a month's entries. Taking all those assumptions into account perhaps you can determine how to adjust the code for your needs if the assumptions are not accurate:
Code:
Sub DetailConsolidation()
Dim WS As Worksheet, WS1 As Worksheet, WB As Workbook

Set WS1 = ActiveWorkbook.Worksheets("Return")
For Each WS In ActiveWorkbook.Worksheets
    If WS.Name <> WS1.Name Then
        WS.Range("A2:N" & WS.Cells(Rows.Count, 1).End(xlUp).Row).Copy WS1.Cells(Rows.Count, 1).End(xlUp).Offset(1)
    End If
Next
If WorksheetFunction.CountIf(WS1.Range("A:A"), "GR for acc.assgt rev") > 0 Then
    WS1.Range("A2").AutoFilter Field:=1, Criteria1:="GR for acc.assgt rev"
    WS1.Range("A2", WS1.Cells(Rows.Count, 1).End(xlUp)).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    WS1.Range("A2").AutoFilter
End If

End Sub
 
Upvote 0
Thanks Brian. I just tested. This almost has it, however I do have other sheets in the workbook that I do not want top copy the data from. How would I alter the code to only grab the Jan through Dec worksheets?

To clarify a bit more the "GR for acc.assgt rev" is in column C on worksheets Jan through Dec.

Thanks again, I would have never got this far without the help.
 
Upvote 0
Out of curiosity, how are your sheets set up in your workbook and will that change? Meaning is Jan always located at the first of the Workbook or 2nd, so on and so forth?
 
Upvote 0
Yes it's possible that they could change as I have multiple people who will be using and they may move them on occasion.
 
Upvote 0
Well then perhaps something like this with an array of the sheet names:
Code:
End Sub
Sub DetailConsolidation()
Dim WS As Worksheet, WS1 As Worksheet, WB As Workbook
Dim MyArray

MyArray = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
Set WS1 = ActiveWorkbook.Worksheets("Return")
For x = 0 To 11
    Set WS = ActiveWorkbook.Worksheets(MyArray(x))
    WS.Range("A2:N" & WS.Cells(Rows.Count, 1).End(xlUp).Row).Copy WS1.Cells(Rows.Count, 1).End(xlUp).Offset(1)
Next
If WorksheetFunction.CountIf(WS1.Range("C:C"), "GR for acc.assgt rev") > 0 Then
    WS1.Range("A2", WS1.Cells(Rows.Count, "n").End(xlUp)).AutoFilter Field:=3, Criteria1:="GR for acc.assgt rev"
    WS1.Range("A2", WS1.Cells(Rows.Count, 1).End(xlUp)).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    WS1.Range("A2").AutoFilter
End If

End Sub
 
Upvote 0
One issue I came across. Code is copy blank rows from the Jan-Dec sheets and pasting them onto Return. Is there a way not to copy blank rows on Jan-Dec sheets? I also noticed that code is not pasting row A2 from Jan-Dec sheets.
 
Upvote 0
Code:
WS.Range("A2:N" & WS.Cells(Rows.Count, 1).End(xlUp).Row).Copy
This copies A2 to the last row of the column and uses column N. Thus, it is copying row 2.
It may be removing it if C is showing your stipulated entry of "GR for acc.assgt rev" but that is only case I can get the code to not copy the 2nd row for me.

as for not copying blank rows, if the cell in column A of a row is empty then is the entire row empty?

I will run a few more tests later tonight to make sure code is running as I would expect, but it will have to be much later on my end.
 
Upvote 0
The part of the code for A2:N seems correct to me as well but for some reason it's not pasting it to Return sheet. I verified that Jan-Dec sheets do not have "GR for acc.assgt rev" in column C for that row.

as for not copying blank rows, if the cell in column A of a row is empty then is the entire row empty?

Yes the entire row is blank if cell A2 is blank
 
Upvote 0
So this will take care of your issues with the blank rows:
Code:
Sub DetailConsolidation()
Dim WS As Worksheet, WS1 As Worksheet, WB As Workbook
Dim MyArray, x As Long

MyArray = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
Set WS1 = ActiveWorkbook.Worksheets("Return")
For x = 0 To 11
    Set WS = ActiveWorkbook.Worksheets(MyArray(x))
    WS.Range("A2:N" & WS.Cells(Rows.Count, 1).End(xlUp).row).Copy WS1.Cells(Rows.Count, 1).End(xlUp).Offset(1)
Next
If WorksheetFunction.CountIf(WS1.Range("C:C"), "GR for acc.assgt rev") > 0 Then
    WS1.Range("A2", WS1.Cells(Rows.Count, "n").End(xlUp)).AutoFilter Field:=3, Criteria1:="GR for acc.assgt rev"
    WS1.Range("A2", WS1.Cells(Rows.Count, 1).End(xlUp)).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    WS1.Range("A2").AutoFilter
End If
If WorksheetFunction.CountBlank(WS1.Range("A1", WS1.Cells(Rows.Count, 1).End(xlUp))) > 0 Then
    WS1.Range("A1", WS1.Cells(Rows.Count, 1).End(xlUp)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End If

End Sub

No matter what I have tried to this point, I have not been able to replicate your issue of not copying row 2 yet. Is there anything special about row 2 in your particular data model?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,110
Messages
6,123,140
Members
449,098
Latest member
Doanvanhieu

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