Macro to copy and paste rows based on cell value

uncleslinky

New Member
Joined
Mar 31, 2009
Messages
47
Hi


I'm looking to automate a small copy & paste process with a macro but I'm having problems knowing where to start. I can record a macro to figure out some of copy and paste parts but I'd also need an if statement.


I have Sheet 1 where I want to look in column B to see if the value "YES" occurs, if so I want to copy the 8 adjacent cells - not including column B - so from column C:J for that row. This would then need to be pasted in Sheet 2, Column A at the next available Row.


I would then need to restart the process but this time instead of looking in Column C on Sheet 1 and, I'd need to now look in Column M for "YES" then copy the next 8 adjacent cells as before and again paste values to the next available row in Sheet 2.


I hope this makes sense (and that I've not missed any detail)
Any input would be great


Thanks
Rich
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Code:
Sub copyStuff()
Dim sh1 As Worksheet, sh2 As Worksheet, lr As Long
Set sh1 = Sheets(1) 'Edit sheet name
Set sh2 = Sheets(2) 'Edit sheet name
lr = sh1.Cells.Find("*", , xlFormulas, xlPart, xlByRows, xlPrevious).Row
    With sh1
        .Range("B1:J" & lr).AutoFilter 1, "Yes"
        .Range("C2:J" & lr).SpecialCells(xlCellTypeVisible).Copy sh2.Cells(Rows.Count, 1).End(xlUp)(2)
        .AutoFilterMode = False
        .Range("M1:U" & lr).AutoFilter 1, "Yes"
        .Range("N2:U" & lr).SpecialCells(xlCellTypeVisible).Copy sh2.Cells(Rows.Count, 1).End(xlUp)(2)
        .AutoFilterMode = False
    End With
End Sub

Goes in standard code module 1.
 
Upvote 0
Hi

Thanks for the reply and solution, that's working great
Can I just ask one more thing, I forgot to cover error checking - is there a way to if "YES" cannot be found in Column B then move on to the next part of the code, as at the moment an error is occurring when "YES" is not present?

Thanks again
 
Upvote 0
Hi

Thanks for the reply and solution, that's working great
Can I just ask one more thing, I forgot to cover error checking - is there a way to if "YES" cannot be found in Column B then move on to the next part of the code, as at the moment an error is occurring when "YES" is not present?

Thanks again
Code:
Sub copyStuff()
Dim sh1 As Worksheet, sh2 As Worksheet, lr As Long
Set sh1 = Sheets(1) 'Edit sheet name
Set sh2 = Sheets(2) 'Edit sheet name
lr = sh1.Cells.Find("*", , xlFormulas, xlPart, xlByRows, xlPrevious).Row
    With sh1
        If Application.CountIf(.Range("B:B"), "yes") > 0 Then
            .Range("B1:J" & lr).AutoFilter 1, "Yes"
            .Range("C2:J" & lr).SpecialCells(xlCellTypeVisible).Copy sh2.Cells(Rows.Count, 1).End(xlUp)(2)
        End If
        .AutoFilterMode = False
        If Application.CountIf(.Range("M:M"), "yes") > 0 Then
            .Range("M1:U" & lr).AutoFilter 1, "Yes"
            .Range("N2:U" & lr).SpecialCells(xlCellTypeVisible).Copy sh2.Cells(Rows.Count, 1).End(xlUp)(2)
        End If
        .AutoFilterMode = False
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,868
Members
449,054
Latest member
juliecooper255

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