If value in sheet1 column A is found in Sheet2 column A, Copy that whole row from sheet1 and replace the value in sheet2

JaimeMabini

New Member
Joined
Dec 29, 2021
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am new on creating Excel VBA, any assistance would be highly appreciated.

I am trying to create a VBA that will read the entire Sheet1 and find matches in sheet2. If the value in sheet1 matches a value in sheet2 in any row order, copy the and replace the row found in sheet2 with the value from sheet1.

Example of what I am trying to do (note that the sheet contains massive data with formula on some cells)

Sheet1:

Row1mmValue1
Row2mmValue2
Row3mmValue3

Sheet2:
Row4mmValue4
Row1mmValue5
Row5mmValue6
Row3mmValue7

After running the VBA Code:

Sheet2:
Row4mmValue4
Row1mmValue1
Row5mmValue6
Row3mmValue3

Result is that Row1 and Row3 in sheet1 is found in sheet2, and therefore after running the code sheet2 will be updated with the value found in sheet1
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hi and welcome to MrExcel.

Try this:

VBA Code:
Sub UpdateSheet2()
  Dim i As Long
  Dim f As Range, c As Range
  
  Application.ScreenUpdating = False
  With Sheets("Sheet2")
    For Each c In .Range("A1", .Range("A" & Rows.Count).End(3))
      Set f = Sheets("Sheet1").Range("A:A").Find(c.Value, , xlValues, xlWhole, , , False)
      If Not f Is Nothing Then
        f.EntireRow.Copy
        .Range("A" & c.Row).PasteSpecial xlValues
      End If
    Next
  End With
  Application.CutCopyMode = False
End Sub
 
Upvote 0
Solution
Hello Dante!

Thank you for the warm welcome! and this is exactly what I am looking for. Big Thanks! ;)
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0
Hello Dante,

With this code of yours:

VBA Code:
Sub UpdateSheet2()
  Dim i As Long
  Dim f As Range, c As Range
  
  Application.ScreenUpdating = False
  With Sheets("Sheet2")
    For Each c In .Range("A1", .Range("A" & Rows.Count).End(3))
      Set f = Sheets("Sheet1").Range("A:A").Find(c.Value, , xlValues, xlWhole, , , False)
      If Not f Is Nothing Then
        f.EntireRow.Copy
        .Range("A" & c.Row).PasteSpecial xlValues
      End If
    Next
  End With
  Application.CutCopyMode = False
End Sub

Is there a way to limit the job to somehow create a dynamic range from an inputted value in sheet1 e.g. A3 that will stop the job in that range. For example, I inputted A3 in cell D1 in sheet1. The copy job to sheet2 will end or exit once the range A3 was meant despite having for example A500 rows.

Sheet1
Column A
Column B
Column C
row 1abc1A3
row 2efg12
row 3hij3
row 4klm4

Sheet2
Column A
Column B
Column C
abc7
efg8
hij9
klm10

Result of Sheet2 after running the code
abc1
efg12
hij3
klm10

In this example. Notice that the last row with attributes "klm" still has the same value from sheet2 ="10" despite having value sheet1="4" because we inputted a range A3 in Column C sheet1 and it was not included in the job. Also, I was hoping that when it reach the inputted range, it will exit the job.

I have this code that works with the range, it only copies the value up until the range I inputted. but the job doesn't end or exit automatically.

VBA Code:
Sub UpdateSheet2()


  Dim i As Long
  Dim f As Range, c As Range
  Sheets("Sheet1").Select 'sheet where the inputted range is coming from
  last = Range("B19").Value 'cell where the inputted range is coming from
  
  Application.ScreenUpdating = False
  With Sheets("Sheet2")
    For Each c In .Range("A1", .Range("A" & Rows.Count).End(3))
      Set f = Sheets("Sheet1").Range("A1", last).Find(c.Value, , xlValues, xlWhole, , , False) 'I put the parameter here
      
      If Not f Is Nothing Then
        f.EntireRow.Copy
        .Range("A" & c.Row).PasteSpecial xlValues
        
      End If
    Next
  End With
  Application.CutCopyMode = False
  
End Sub
 
Upvote 0
Hey Dante,

Thanks for the code. I just registered just for this question which is important to me.

Kindly, Is there a way you can paste the whole cell but this time with its highlighted cells (Mine in yellow). So the problem it pastes the row without its format such as color (as some are highlighted) background color

This is the original cell

1645098296950.png

This is the cell after applying your code

1645098265765.png


Basicly, I need it move/replaced with the highlight please.

Looking forward for your replay,
Amru
 
Upvote 0
Hi @amrubinissa and welcome to MrExcel.

Is there a way you can paste the whole cell but this time with its highlighted cells
Try this

VBA Code:
Sub UpdateSheet2()
  Dim i As Long
  Dim f As Range, c As Range
  
  Application.ScreenUpdating = False
  With Sheets("Sheet2")
    For Each c In .Range("A1", .Range("A" & Rows.Count).End(3))
      Set f = Sheets("Sheet1").Range("A:A").Find(c.Value, , xlValues, xlWhole, , , False)
      If Not f Is Nothing Then
        f.EntireRow.Copy .Range("A" & c.Row)
      End If
    Next
  End With
  Application.CutCopyMode = False
End Sub
 
Upvote 0
Hey Dante

I genuinely appreciate it. It worked exactly the way I wanted to.

I wish you a happy day, what’s left of the day.

Many thanks
Amru
 
Upvote 0
Hi again Dante,

Sorry to bother, please I wanted to add one more thing to the code.

I want the whole row to be replaced not just if its match column A but also column J. So in order to completely replace the row, both column A cell and column J cells has to be the same.

Warms regards,
Amru
 
Upvote 0
Hope you are safe & well Dante

Simply stating what I'm looking for:

IF (cells in column A & J together in sheet 1) match (cells in columns A & J together in sheet 2) THEN replace whole sheet 2 row from sheet 2

Any idea how?
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,701
Members
448,980
Latest member
CarlosWin

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