Look for match, copy if no match

femma

Board Regular
Joined
Jul 13, 2016
Messages
156
Hi Forum!

I'm trying to copy all the values that are not found into column J. But get an error at the line
Code:
Selection.Copy Destination:=Sheets(1).Range(lastrange).Paste
Any ideas why?


Code:
Option Explicit
Sub codeforP()
Dim lastrange As Long
Dim lastrow1 As Long
Dim lastrow2 As Long
Dim business As Variant
Dim i As Long
Dim a As Long
lastrange = Sheets(1).Range("J" & Rows.Count).End(xlUp).Row
lastrow1 = Sheets(1).Range("I50000").End(xlUp).Row
lastrow2 = Sheets(1).Range("F57000").End(xlUp).Row

For i = 2 To lastrow1
    business = Sheets(1).Cells(i, "I").Value
    For a = 1 To lastrow2
        If Sheets(1).Cells(a, "F").Value = business Then
            
        Else

        Sheets(1).Cells(a, "F").Select
        Selection.Copy Destination:=Sheets(1).Range(lastrange).Paste
        
        
        
        
        End If
        
    Next a
Next i

MsgBox "ok"

End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Couple of problems. It doesnt need the .Paste and also lastrange is just an integer so you are basically saying as an example Range(1) which makes no sense. Maybe also you want to paste into the lastrange + 1 (ie the cell below the last used cell)? Anyway something like this perhaps:

Code:
Selection.Copy Destination:=Sheets(1).Range("J" & lastrange +1)
 
Upvote 0
Untested (so use on a copy of your data) but this should work and will run much faster than your approach...

Code:
Sub codeforP()
Dim Last_Row As Long
Dim i As Long

Application.ScreenUpdating = False

With Sheets(1)
    Last_Row = Cells(Rows.Count, 9).End(xlUp).Row
    For i = 2 To Last_Row
        If .Cells(i, 6) = .Cells(i, 9) Then .Cells(i, 6).Copy .Cells(i, 10).End(xlUp).Offset(1)
    Next i
End With
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Thanks for the replies!
njimack - what should I do if I want to copy the values that are not matches - those that are not found?
 
Upvote 0
Ah my bad. Change this​
Code:
If .Cells(i, 6) = .Cells(i, 9) Then .Cells(i, 6).Copy .Cells(i, 10).End(xlUp).Offset(1)
to this
Code:
If .Cells(i, 6) [COLOR=#FF0000][B]<>[/B][/COLOR] .Cells(i, 9) Then .Cells(i, 6).Copy .Cells(i, 10).End(xlUp).Offset(1)
 
Upvote 0
Another option
Code:
Sub codeforP()

Dim UsdRws As Long
Dim Cl As Range

With Sheets(1)
    For Each Cl In .Range("F2", .Range("F" & Rows.Count).End(xlUp))
        If WorksheetFunction.CountIf(.Columns(9), Cl.Value) = 0 Then
            .Range("J" & Rows.Count).End(xlUp).Offset(1).Value = Cl.Value
        End If
    Next Cl
End With
MsgBox "ok"

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

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