Copy and paste row if condition is met

mattlim

New Member
Joined
Dec 9, 2020
Messages
32
Office Version
  1. 2019
Platform
  1. MacOS
I know this is a common question asked and answered but i just cant find one that works for me. So I would like the entire row to be copied from sheet 1 if the condition "Children" is met in column D to sheet 2, row 2. Sheet 1 is being constantly updated, so it would be great if the macro doesn't duplicate data but only inserts the new entries to sheet 2. Same goes for the condition "Ignite" in column D copied to sheet 3.

I have zero knowledge in VBA but I need to sort some data. The only thing I know to do is create the button, but without the macro its useless haha. Any help is greatly appreciated. The picture is just a sample, since IC numbers are confidential.
 

Attachments

  • Screenshot 2020-12-09 at 5.23.21 PM.png
    Screenshot 2020-12-09 at 5.23.21 PM.png
    213.2 KB · Views: 118

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary). Please include more data in your sheet so that it is exactly representative of your actual sheet. We would need to know all the criteria that will be used to determine which rows are to be copied. Is it only "Children" and "Ignite"? Also to which sheet the row needs to be pasted for each criteria. To make sure that data is not duplicated, each row of data needs a unique identifier. Column B, the name, cold be a unique identifier if you know for sure that there will be no duplicate names.
 
Upvote 0
It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary). Please include more data in your sheet so that it is exactly representative of your actual sheet. We would need to know all the criteria that will be used to determine which rows are to be copied. Is it only "Children" and "Ignite"? Also to which sheet the row needs to be pasted for each criteria. To make sure that data is not duplicated, each row of data needs a unique identifier. Column B, the name, cold be a unique identifier if you know for sure that there will be no duplicate names.
masterlist sample.xlsm

So based on the program selected in column H (BLESS, children, ignite, literacy, care, adhoc), I'd like the row with only specific columns copied to the allocated program sheet, all starting from row 2, column B. The specific columns are B, C, D, E, F, G, H, and M.
 
Upvote 0
Start by placing the headers in row 1 of each of the destination sheets. Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your Masterlist sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Enter a value in column Q and press the RETURN key or TAB key. I would suggest that you insert a data validation drop down list in column Q. This way you could simply make a selection without having to press the RETURN key or TAB key. Hopefully, this macro will work for you as you are using a Mac and the VBA for Mac's is a little different from the VBA for Windows. I used the email address as the unique identifier assuming that there will be no two email address that are the same.
 
Upvote 0
Start by placing the headers in row 1 of each of the destination sheets. Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your Masterlist sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Enter a value in column Q and press the RETURN key or TAB key. I would suggest that you insert a data validation drop down list in column Q. This way you could simply make a selection without having to press the RETURN key or TAB key. Hopefully, this macro will work for you as you are using a Mac and the VBA for Mac's is a little different from the VBA for Windows. I used the email address as the unique identifier assuming that there will be no two email address that are the same.
You forgot to paste the code haha
 
Upvote 0
I'm getting old and my mind seems to fail me sometimes. :)
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("Q:Q")) Is Nothing Then Exit Sub
    If Target.Count > 1 Then Exit Sub
    Dim fnd As Range
    Application.ScreenUpdating = False
    Set fnd = Sheets(Target.Value).Range("D:D").Find(Target.Offset(, -12).Value, LookIn:=xlValues, lookat:=xlWhole)
    If fnd Is Nothing Then
        With Sheets(Target.Value)
            Intersect(Rows(Target.Row), Range("B:H,M:M")).Copy .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
        End With
    Else
        MsgBox ("The data for " & Target.Offset(, -15) & " " & Target.Offset(, -14) & " already exists in sheet '" & Target.Value & "'.")
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
I'm getting old and my mind seems to fail me sometimes. :)
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("Q:Q")) Is Nothing Then Exit Sub
    If Target.Count > 1 Then Exit Sub
    Dim fnd As Range
    Application.ScreenUpdating = False
    Set fnd = Sheets(Target.Value).Range("D:D").Find(Target.Offset(, -12).Value, LookIn:=xlValues, lookat:=xlWhole)
    If fnd Is Nothing Then
        With Sheets(Target.Value)
            Intersect(Rows(Target.Row), Range("B:H,M:M")).Copy .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
        End With
    Else
        MsgBox ("The data for " & Target.Offset(, -15) & " " & Target.Offset(, -14) & " already exists in sheet '" & Target.Value & "'.")
    End If
    Application.ScreenUpdating = True
End Sub
The code works! Just a tiny problem with it. Not everyone keyed in their email. So for those without emails, because email is considered as the unique identifier, those without emails wont be copied over and a message saying that person's data already exists. So if I enter two person's data without emails back to back, the second person's data won't be copied. Is there a fix to this?
 
Upvote 0
If there isn't a way around that, the unique identifier can be changed to name in column C
 
Upvote 0
Are you saying that if there is no email in column E you don't want an email created for that person? Please explain in detail how you want to handle people with no email. Using the name in column C could be problematic if there are more than one person with the same name.
 
Upvote 0
Are you saying that if there is no email in column E you don't want an email created for that person? Please explain in detail how you want to handle people with no email. Using the name in column C could be problematic if there are more than one person with the same name.
I would still want those without email to be copied over to the relevant program (ignite, bless etc). But because there are multiple people without emails, the error message shows up and it doesnt copy over since it isnt unique.
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,237
Members
448,555
Latest member
RobertJones1986

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