Macro to copy rows from multiple worksheets into one worksheet if criteria is met

bmac206

New Member
Joined
Dec 13, 2012
Messages
6
I have a spreadsheet that has over a dozen worksheets. Each worksheet has the same format with column headers from A1:G1 and then data underneath. Each worksheet has a varying number of rows with data. Column G is a dropdown box of either "Yes" or "No". I want to create a macro that will copy all the rows on each worksheet only if Column G is equal to "Yes" and combine them on a sheet I am calling "Master". This Master sheet will have the same column headers from A1:G1.

I would also need the macro to delete what it had previously pasted in the Master sheet (everything below A1:G1)

Much appreciated if anyone can help. Thanks!
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
I have a spreadsheet that has over a dozen worksheets. Each worksheet has the same format with column headers from A1:G1 and then data underneath. Each worksheet has a varying number of rows with data. Column G is a dropdown box of either "Yes" or "No". I want to create a macro that will copy all the rows on each worksheet only if Column G is equal to "Yes" and combine them on a sheet I am calling "Master". This Master sheet will have the same column headers from A1:G1.

I would also need the macro to delete what it had previously pasted in the Master sheet (everything below A1:G1)

Much appreciated if anyone can help. Thanks!
This assumes that your master sheet is the left-most sheet tab in the workbook (sheet(1)).
Code:
Sub bmac206()
Dim nR As Long, R As Range
'The Master sheet must be the first sheet in the workbook
With Sheets(1).Range("A1").CurrentRegion.Offset(1, 0)
    .ClearContents
End With
Application.CutCopyMode = False
For i = 2 To Sheets.Count
    With Sheets(i).Range("A1").CurrentRegion
        .AutoFilter field:=Columns("G").Column, Criteria1:="yes"
        On Error Resume Next
        Set R = .SpecialCells(xlCellTypeVisible)
        If Not R Is Nothing Then
            R.Offset(1, 0).Copy
            With Sheets(1)
                nR = .Range("A" & Rows.Count).End(xlUp).Row + 1
                .Range("A" & nR).PasteSpecial Paste:=xlValues
                Application.CutCopyMode = False
            End With
        End If
        .AutoFilter
    End With
Next i
End Sub
 
Upvote 0
Thanks! It seems to be almost working but some of the rows that say "Yes" are not getting copied. Specifically, if a row has a "No" then the very next row (whether column G has a "Yes" or a "No") it will not get copied to the master. Any ideas?
 
Upvote 0
Thanks! It seems to be almost working but some of the rows that say "Yes" are not getting copied. Specifically, if a row has a "No" then the very next row (whether column G has a "Yes" or a "No") it will not get copied to the master. Any ideas?
I don't understand the bold part in the quote. If the next row is a "no" you don't want it copied do you?

If it's just missing some "yes" rows you might want to verify that the "yes" in the row has no spaces before or after it. You can do this in various ways. For now, suppose G5 has "yes" and didn't get copied, then in an empty cell enter =LEN(G5) and see what number is returned. If its not 3 then there are extra characters (probably spaces) in that cell and Excel will not see the cell as having "yes" in it.

If that's not the problem, then you can step through the code and see how the filter is being applied to try to diagnose what's happening. I tested the code and found no issues.
 
Upvote 0
Try
Code:
Sub MM1()
Dim r As Long, lr As Long, lr2 As Long, ws As Worksheet
lr = Sheets("Master").Cells(Rows.Count, "A").End(xlUp).Row
Sheets("Master").Range("A2:G" & lr).ClearContents
For Each ws In Worksheets
    If ws.Name <> "Master" Then ws.Activate
    lr2 = Cells(Rows.Count, "A").End(xlUp).Row
        For r = 2 To lr2
            If Range("G" & r).Value = "Yes" Then
                Rows(r).Copy Destination:=Sheets("Master").Range("A" & lr + 1)
                lr = Sheets("Master").Cells(Rows.Count, "A").End(xlUp).Row
            End If
        Next r
Next ws
End Sub
 
Upvote 0
bmac206,

Welcome to the MrExcel forum.


Sample worksheets:


Excel Workbook
ABCDEFG
1ABCDEFG
2
3
4
5
6
7
8
9
10
11
Master





Excel Workbook
ABCDEFG
1ABCDEFG
2
3Sheet1B3C3D3E3F3Yes
4
5Sheet1B5C5D5E5F5Yes
6
7Sheet1B7C7D7E7F7Yes
8
Sheet1





Excel Workbook
ABCDEFG
1ABCDEFG
2
3
4Sheet2B4C4D4E4F4Yes
5
6Sheet2B6C6D6E6F6Yes
7
8Sheet2B8C8D8E8F8Yes
9
Sheet2





Excel Workbook
ABCDEFG
1ABCDEFG
2
3
4
5
6
7
8
9
10Sheet4B10C10D10E10F10Yes
11
12Sheet4B12C12D12E12F12Yes
13
14Sheet4B14C14D14E14F14Yes
15
Sheet4





After the macro (which took 0.125 seconds to run on my Lenovo T61 laptop computer):


Excel Workbook
ABCDEFG
1ABCDEFG
2Sheet1B3C3D3E3F3Yes
3Sheet1B5C5D5E5F5Yes
4Sheet1B7C7D7E7F7Yes
5Sheet2B4C4D4E4F4Yes
6Sheet2B6C6D6E6F6Yes
7Sheet2B8C8D8E8F8Yes
8Sheet4B10C10D10E10F10Yes
9Sheet4B12C12D12E12F12Yes
10Sheet4B14C14D14E14F14Yes
11
Master





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


Code:
Option Explicit
Sub GetYes()
' hiker95, 12/13/2012
' http://www.mrexcel.com/forum/excel-questions/674560-macro-copy-rows-multiple-worksheets-into-one-worksheet-if-criteria-met.html
Dim wM As Worksheet, ws As Worksheet
Dim r As Long, lr As Long, nr As Long, y As Long
Dim c As Range, firstaddress As String
Application.ScreenUpdating = False
Set wM = Sheets("Master")
lr = wM.Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False).Row
If lr > 1 Then wM.Range("A2:G" & lr).ClearContents
For Each ws In ThisWorkbook.Worksheets
  If ws.Name <> "Master" Then
    y = 0
    On Error Resume Next
    y = Application.CountIf(ws.Columns(7), "Yes")
    On Error GoTo 0
    If y > 1 Then
      firstaddress = ""
      With ws.Columns(7)
        Set c = .Find("Yes", LookIn:=xlValues, LookAt:=xlWhole)
        If Not c Is Nothing Then
          firstaddress = c.Address
          Do
            nr = wM.Range("G" & Rows.Count).End(xlUp).Offset(1).Row
            ws.Range("A" & c.Row & ":G" & c.Row).Copy wM.Range("A" & nr)
            Application.CutCopyMode = False
            Set c = .FindNext(c)
          Loop While Not c Is Nothing And c.Address <> firstaddress
        End If
      End With
    End If
  End If
Next ws
wM.Activate
Application.ScreenUpdating = True
End Sub


Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm


Then run the GetYes macro.
 
Upvote 0
I have a spreadsheet that has over a dozen worksheets. Each worksheet has the same format with column headers from A1:G1 and then data underneath. Each worksheet has a varying number of rows with data. Column G is a dropdown box of either "Yes" or "No". I want to create a macro that will copy all the rows on each worksheet only if Column G is equal to "Yes" and combine them on a sheet I am calling "Master". This Master sheet will have the same column headers from A1:G1.

I would also need the macro to delete what it had previously pasted in the Master sheet (everything below A1:G1)

Much appreciated if anyone can help. Thanks!
Same post also made here
Macro to copy rows from multiple worksheets into one worksheet if criteria is met
where an answer is given.
 
Upvote 0
I was using a drop down box so the "Yes" and "No" were the same. I tested several times with no luck. Thanks.
 
Upvote 0
Micheal - The macro you wrote seemed to be working for the most part but when i would run the report a second time, the new data would paste below where the old data was on the Master (even though the cell contents were deleted). Thanks for the help though.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,100
Messages
6,128,825
Members
449,470
Latest member
Subhash Chand

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