Copy and Paste if Condition is met (the cell contains a certain sentence)

gingerbreadgrl

New Member
Joined
Aug 19, 2019
Messages
48
Hi All,

I am very new to this and have been limping along with my macro through the stuff I have learned in this forum, on the web, and on youtube. I am trying to copy and paste a range of cells from one worksheet to another in a workbook if a certain cell contains a certain phrase. My code keeps returning an error. The code is below. Does anyone know what I am doing wrong? Thanks in advance!

[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]If Sheets("C2-LTG").Range(B2).Value = "All of my choices are the same as my partner's" Then[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Sheets("C1-LTG").Range("C2:DR2").Copy _
Destination:=Sheets("C2-LTG").Range("D2:DS2")[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]
End If
[/FONT]
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi & welcome to MrExcel.
Try
Code:
If Sheets("C2-LTG").Range(B2).Value = "All of my choices are the same as my partner's" Then
   Sheets("C1-LTG").Range("C2:DR2").Copy Sheets("C2-LTG").Range("D2")
End If
 
Upvote 0
Hi & welcome to MrExcel.
Try
Code:
If Sheets("C2-LTG").Range(B2).Value = "All of my choices are the same as my partner's" Then
   Sheets("C1-LTG").Range("C2:DR2").Copy Sheets("C2-LTG").Range("D2")
End If

Hi,

Thanks so much for responding. I tried your code and it gave me the error 400 message. I'm not sure what that means, bummer.

Best,
Gingerbreadgrl
 
Upvote 0
The 400 is a PITA & doesn't really mean anything of any worth.

Place the cursor anywhere in your code & step through the code using F8.
What error number & message do you get & what line gives the error?
 
Upvote 0
Hi,

Okay so I went through the code and on the "if" line I received the Run-time error'1004': Application-defined or object-defined error. Hopefully that will be more helpful.

Thanks!
 
Upvote 0
Ok, missed that.
You're missing the "s around the range
Code:
If Sheets("C2-LTG").Range([COLOR=#ff0000]"[/COLOR]B2[COLOR=#ff0000]"[/COLOR]).Value = "All of my choices are the same as my partner's" Then
   Sheets("C1-LTG").Range("C2:DR2").Copy Sheets("C2-LTG").Range("D2")
End If
 
Upvote 0
Oh my goodness it worked! Thank you so much, you are a Rockstar!

So I'm just curious I read a whole article on the destination function and that you need that function to paste cells to a certain destination instead of just having the cells copied to the clipboard. I was able to accomplish the copy and paste with your code and I didn't need to use the destination function. Based on your code I just needed to put the first cell of where I wanted the cells pasted (instead of the cell range with the destination function). Why is that? I'm just trying to get a better understanding of how all of this works.

Thank you so much! You made my day!
 
Upvote 0
That code does use Copy Destination, but you don't need to put in the name of the argument so
Code:
Sheets("C1-LTG").Range("C2:DR2").Copy Sheets("C2-LTG").Range("D2")
is exactly the same as
Code:
Sheets("C1-LTG").Range("C2:DR2").Copy Destination:=Sheets("C2-LTG").Range("D2")
When copying cells in this manner I find it's easier to just put in the starting point of the destination range, as it saves trying to work out the full destination range.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,560
Messages
6,120,222
Members
448,951
Latest member
jennlynn

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