If Statement to paste into another sheet Help

MurraySMTH

Board Regular
Joined
Jun 30, 2020
Messages
81
Office Version
  1. 2016
Platform
  1. Windows
Hello all,

I have a code that i wrote to note that A2 and B2 are equal on Sheet 4,

If J2 on a another sheet (Template Upload) will match A2 on (Sheet4) then paste B2 of (Sheet 4) to replace J2 sheet (Template Upload)

Sub Sheet4_templateupload()

Worksheets("Sheet4").Range("A2").Value = Worksheets("Sheet4").Range("B2").Value

If Worksheets("Sheet4").Range("A2").Value = Worksheets("Template Upload").Range("J2").Value Then

Worksheets("Sheet4").Range("B2").Copy _
.Paste Destination:=Worksheets("Template Upload").Range("J2")

End If

End Sub

This is Sheet 4



ColumnA ColumnB
1596130115526.png


Template Upload


1596132130279.png


In summary, If Column A of Sheet 4 and Column J of the Template Upload Sheet match, then replace with B2 with the match of A2 of sheet 4.

Hope this is clear!
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
You don't use the .Paste when using Destination:=, just...

VBA Code:
Worksheets("Sheet4").Range("B2").Copy _
Destination:=Worksheets("Template Upload").Range("J2")
or
VBA Code:
Worksheets("Sheet4").Range("B2").Copy _
Worksheets("Template Upload").Range("J2")

P.S. can you please use code tags when posting code rather than changing the font (especially to a fainter font)
 
Upvote 0
Thank you for the note, The vba is not matching the J2 or pasting it in J2, any tips?


VBA Code:
Sub Sheet4_templateupload()

Worksheets("Sheet4").Range("A2").Value = Worksheets("Sheet4").Range("B2").Value

If Worksheets("Sheet4").Range("A2").Value = Worksheets("Template Upload").Range("J2").Value Then

Worksheets("Sheet4").Range("B2").Copy _
Destination:=Worksheets("Template Upload").Range("J2")

End If

End Sub

Testing to see if it pasted the way that you prefer.
 
Upvote 0
Are you sure that the values in the cells exactly match i.e. expand the cells format to 15 decimal places and check
 
Upvote 0
Yes all good there. The only thing that happens is A2 turns into B2's value... but no pasting to the next sheet. very weird.
 
Upvote 0
I have just set up 2 sheets with the names that you have stated and ran
VBA Code:
Sub Sheet4_templateupload()

'Worksheets("Sheet4").Range("A2").Value = Worksheets("Sheet4").Range("B2").Value

If Worksheets("Sheet4").Range("A2").Value = Worksheets("Template Upload").Range("J2").Value Then

Worksheets("Sheet4").Range("B2").Copy _
Destination:=Worksheets("Template Upload").Range("J2")

End If

End Sub

Worksheets("Template Upload").Range("J2") populated with the contents of Worksheets("Sheet4").Range("B2") when Worksheets("Template Upload").Range("J2").Value matched Worksheets("Sheet4").Range("A2").Value.

Step through the code with F8, what happens after you reach the If statement?
 
Upvote 0
Ok i took away the statement:

VBA Code:
'Worksheets("Sheet4").Range("A2").Value = Worksheets("Sheet4").Range("B2").Value
like you suggested! it worked... Now i will fill in for the rest of the data.

Thanks for your quick help, and the note about posting in the correct forum format.
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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