VBA formula that will insert a column in sheet2 and shows if the row is copied from sheet1.

JaimeMabini

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

I have this VBA formula that compares the attributes in sheet1 to sheet2.
  • If attributes in sheet1 is found in sheet2, copy the entire row and paste in sheet2.

    VBA Code:
    Sub UpdateSheet2()
    
    On Error Resume Next
    
      Dim i As Long
      Dim f As Range, c As Range
      Sheets("Sheet1").Select
      last = Range("B19").Value
      
      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)
          
          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

    Code is perfectly working.

    Now what I need to do/add is insert a column in Sheet2 that will show "Yes" if the row is copied from sheet1 and "No" if not.

    Thank you. Any help will be highly appreaciated.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Note: dont use this instruction: On Error Resume Next. If the macro has a problem, you won't know where in which problem.

In column Z the results will be Yes / No
Try this:
VBA Code:
Sub UpdateSheet2()
  Dim i As Long
  Dim f As Range, c As Range
  Sheets("Sheet1").Select
  last = Range("B19").Value
  
  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)
      
      If Not f Is Nothing Then
        f.EntireRow.Copy
        .Range("A" & c.Row).PasteSpecial xlValues
        .Range("Z" & c.row).value = "Yes"
      Else
        .Range("Z" & c.row).value = "No"
      End If
    Next
  End With
  Application.CutCopyMode = False
  
End Sub
 
Upvote 0
Solution
Note: dont use this instruction: On Error Resume Next. If the macro has a problem, you won't know where in which problem.

In column Z the results will be Yes / No
Try this:
VBA Code:
Sub UpdateSheet2()
  Dim i As Long
  Dim f As Range, c As Range
  Sheets("Sheet1").Select
  last = Range("B19").Value
 
  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)
     
      If Not f Is Nothing Then
        f.EntireRow.Copy
        .Range("A" & c.Row).PasteSpecial xlValues
        .Range("Z" & c.row).value = "Yes"
      Else
        .Range("Z" & c.row).value = "No"
      End If
    Next
  End With
  Application.CutCopyMode = False
 
End Sub
Note: dont use this instruction: On Error Resume Next. - Noted.

And this code is perfectly working! You are Awesome!!! Thank you ?
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,164
Members
453,021
Latest member
Justyna P

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