Pasting into worksheet from another worksheet's data. Help

MurraySMTH

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

I am having trouble with my code. I am trying to write a code that recognizes that Sheet 4 column A&B match each other though they contain different data.

So if column A on "sheet 4" and column J on sheet "Template upload" match, then paste column B on Sheet 4 to replace column J on Sheet "template upload".

Sheet 4 Sheet "Template Upload"
1596158848402.png
1596158875522.png


My code better explains the method i am going for.

VBA Code:
 Sub LocationCodes_to_ColumnJ()
  
    a = Worksheets("Sheet4").Cells(Rows.Count, 1).End(xlUp).Row
    
    
    For i = 2 To a
    
    Next
    
    If Worksheets("Sheet4").Cells(i, 1).Value = Worksheets("Sheet4").Cells(i, 2).Value Then
    
    
    Worksheets("Sheet4").Cells(i, 2).Value = Worksheets("Template Upload").Cells(i, 10).Value
  
    Worksheets("Sheet4").Cells(i, 2).Copy
    
 Destination = Worksheets("Template Upload").Cells(i, 10)
 
End If

End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try this on a copy of your data.
VBA Code:
Sub Codes_to_J()
Dim ShFrom As Worksheet, ShTo As Worksheet, lr As Long, i As Long
    Set ShFrom = ActiveWorkbook.Worksheets("Sheet4")
    Set ShTo = ActiveWorkbook.Worksheets("Template Upload")
    
    lr = ShFrom.Cells(Rows.Count, 1).End(3).Row
    
    With ShFrom
        For i = 2 To lr
            Dim ColACell As String
            ColACell = .Cells(i, 1).Value
            If ColACell <> .Cells(i, 2).Value Then ShTo.Cells(i, 10).Value = ColACell
        Next i
    End With
End Sub
 
Upvote 0
Hi Lazy Bug, This pasted A in Sheet 4 to the J column Template upload Sheet... So basically A = B (Sheet 4) and if J ("Template Upload")= A (Sheet 4) paste the values in B (Sheet 4) to J (Template Upload).

So replace J (Template Upload) with B (Sheet 4) only if B matches with A (Sheet 4).
 
Upvote 0
So replace J (Template Upload) with B (Sheet 4) only if B matches with A (Sheet 4).
Hello! What about
VBA Code:
Sub Codes_to_J()
Dim ShFrom As Worksheet, ShTo As Worksheet, lr As Long, i As Long
    Set ShFrom = ActiveWorkbook.Worksheets("Sheet4")
    Set ShTo = ActiveWorkbook.Worksheets("Template Upload")
    
    lr = ShFrom.Cells(Rows.Count, 1).End(3).Row
    
    With ShFrom
        For i = 2 To lr
            Dim ColBCell As String
            ColBCell = .Cells(i, 2).Value
            If ColBCell = .Cells(i, 1).Value Then ShTo.Cells(i, 10).Value = ColBCell
        Next i
    End With
End Sub
 
Upvote 0
Thank you so much Lazy Bug! You have been such a wonderful help.
 
Upvote 0
You are welcome, thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,430
Messages
6,124,849
Members
449,194
Latest member
HellScout

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