Compare and Copy Data from Columns in One Sheet to Another Sheet

saltedhoneylatte

New Member
Joined
Jul 29, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I am looking for a VBA code/macros that does the following...

Compare the data in a cell from Column A from Sheet 1 to the data in a cell in Column B in Sheet 2. If the data values are equal, then I want to take the values in the row from Column D in Sheet 1 and past them into the row in Column K in Sheet 2, then move to the next row in sheet 1 to compare. Sheet 1 and Sheet 2 rows may not align.

Column B in sheet 2 has all the values that could possibly exist in Column A in sheet 1.

I've included a table to try to help visualize what I am trying to do..

Sheet 1
AD
data to comparecell to copy

Sheet Two
BK
data to comparecell to paste
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Why would VBA be needed for this? Seems like an easy enough VLOOKUP would suffice. In column K on sheet2:

=VLOOKUP(B2,Sheet1!A:D,4,0)

If you want to hide errors when the value isn't found on Sheet1, then wrap it in an IFERROR function:

=IFERROR(VLOOKUP(B2,Sheet1!A:D,4,0),"")
 
Upvote 0
I’m t
Why would VBA be needed for this? Seems like an easy enough VLOOKUP would suffice. In column K on sheet2:

=VLOOKUP(B2,Sheet1!A:D,4,0)

If you want to hide errors when the value isn't found on Sheet1, then wrap it in an IFERROR function:

=IFERROR(VLOOKUP(B2,Sheet1!A:D,4,0),
 
Upvote 0
Why would VBA be needed for this? Seems like an easy enough VLOOKUP would suffice. In column K on sheet2:

=VLOOKUP(B2,Sheet1!A:D,4,0)

If you want to hide errors when the value isn't found on Sheet1, then wrap it in an IFERROR function:

=IFERROR(VLOOKUP(B2,Sheet1!A:D,4,0),"")
I am trying to replace values in sheet two. I run a macros to pull items from sheet two into sheet one. Then, we may change values in sheet one and I want to push the new values back to sheet two.
 
Upvote 0
Maybe something like this?
VBA Code:
Sub latte()
Dim i As Long, lastrow_1 As Long, lastrow_2 As Long, ws1 As Worksheet, ws2 As Worksheet
lastrow_1 = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
lastrow_2 = Sheets("Sheet2").Range("B" & Rows.Count).End(xlUp).Row
Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")

For i = 1 To lastrow_2
    With Application.WorksheetFunction
        On Error Resume Next
        ws2.Range("K" & i).Value = .VLookup(ws2.Range("B" & i).Value, ws1.Range("A1:D" & lastrow_1), 4, False)
        On Error GoTo 0
    End With
Next i
End Sub
 
Upvote 0
I am a little confused.. I kept the data general and I'm struggling to put it into my actual form.. Do you mind explaining what each does?
 
Upvote 0

Forum statistics

Threads
1,223,702
Messages
6,173,957
Members
452,539
Latest member
delvey

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