VBA copy row to new sheet if cell value isn't found in range

Magoosball

Board Regular
Joined
Jun 4, 2017
Messages
70
Office Version
  1. 365
Hello I'm trying to write a macro that will copy an entire row from sheet2 and paste it to the bottom of sheet1 if the cell value in column f sheet 2 doesn't exist at all in column f on sheet 1.

For example if column f sheet 2 = 123456 and 123456 doesn't exist anywhere in sheet1 column f then copy the entire row from sheet2 and paste it to the bottom of sheet1.

This will have to exist in a loop for all rows on sheet 2.

Thanks in advance!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Re: VBA copy row to new sheet if cell value isn't found in rance

Untested, but try
Code:
Sub Magoo()
   Dim Cl As Range, Rng As Range
   Dim Ws1 As Worksheet, Ws2 As Worksheet
   
   Set Ws1 = Sheets("sheet1")
   Set Ws2 = Sheets("sheet2")
   With CreateObject("scripting.dictionary")
      For Each Cl In Ws1.Range("F2", Ws1.Range("F" & Rows.Count).End(xlUp))
         .Item(Cl.Value) = Empty
      Next Cl
      For Each Cl In Ws2.Range("F2", Ws2.Range("F" & Rows.Count).End(xlUp))
         If Not .Exists(Cl.Value) Then
            If Rng Is Nothing Then Set Rng = Cl Else Set Rng = Union(Rng, Cl)
         End If
      Next Cl
   End With
   If Not Rng Is Nothing Then
      Rng.EntireRow.Copy Ws1.Range("A" & Rows.Count).End(xlUp).Offset(1)
   End If
End Sub
 
Upvote 0
Solution
Re: VBA copy row to new sheet if cell value isn't found in rance

Thank you Fluff! Like always it works perfect!
 
Upvote 0
Re: VBA copy row to new sheet if cell value isn't found in rance

Glad to help & thanks for the feedback
 
Upvote 0
Re: VBA copy row to new sheet if cell value isn't found in rance

Glad to help & thanks for the feedback
Hi!

I have found this code very useful for a project I'm working on. However, I have a couple of adjustments I'm not sure how to make. For one, when I copy the entire row, it brings over the *entire* row - blank cells all the way to the end of the workbook - which makes scrolling through the destination sheet kind of inconvenient with all the blank cells. Is there a way to copy the row with only data? Right now, I am just doing rng.EntireRow.SpecialCells(xlCellTypeVisible).Copy since I have a hidden column that I don't want copied. This is also part of an actual table (list object).
 
Upvote 0
As this is significantly different, you need to start a thread of your own. Thanks
 
Upvote 0
Re: VBA copy row to new sheet if cell value isn't found in rance

Untested, but try
Code:
Sub Magoo()
   Dim Cl As Range, Rng As Range
   Dim Ws1 As Worksheet, Ws2 As Worksheet
  
   Set Ws1 = Sheets("sheet1")
   Set Ws2 = Sheets("sheet2")
   With CreateObject("scripting.dictionary")
      For Each Cl In Ws1.Range("F2", Ws1.Range("F" & Rows.Count).End(xlUp))
         .Item(Cl.Value) = Empty
      Next Cl
      For Each Cl In Ws2.Range("F2", Ws2.Range("F" & Rows.Count).End(xlUp))
         If Not .Exists(Cl.Value) Then
            If Rng Is Nothing Then Set Rng = Cl Else Set Rng = Union(Rng, Cl)
         End If
      Next Cl
   End With
   If Not Rng Is Nothing Then
      Rng.EntireRow.Copy Ws1.Range("A" & Rows.Count).End(xlUp).Offset(1)
   End If
End Sub
How can you copy and entire row and paste only the values? with this code it also copies the formules.
 
Upvote 0
Please start a thread of your own for this question.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

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