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

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
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,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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