View Single Post
 
Old 07-07-2024, 11:16 AM
Logit Logit is offline Windows 10 Office 2007
Expert
 
Join Date: Jan 2017
Posts: 587
Logit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the rough
Default

The following compares Sheet 1 vs Sheet 2 and finds anything in Sheet 1 that was not in Sheet 2.

I believe for your project you'll need to change (in the macro code) Sheet 1 to reflect Sheet 2 .... and Sheet 2 should be changed to Sheet 1.

Code:
Sub compare()
Application.ScreenUpdating = False
    Sheets(3).Activate  'Go to sheet 3
    Cells.Clear         'and clear all previous results
    
    Range("a1").Select  'set cursor at the top
    
    Sheets(1).Activate  'go to sheet 1
    Range("a1").Select  'begin at the top
    
   
    
    Dim search_for As String   'temp variable to hold what we need to look for
    Dim cnt As Integer         'optional counter to find out how many rows we found
    
    Do While ActiveCell.Value <> ""   'repeat the follwoing loop until it reaches a blank row
        
        search_for = ActiveCell.Offset(0, 1).Value   'get a hold of the value in column B
        
        Sheets(2).Activate  'go to sheet(2)
        
        On Error Resume Next   'incase what we search for is not found, no errors will stop the macro
        
        Range("b:b").Find(search_for).Select  'find the value in column B of sheet 2
        
        If Err <> 0 Then   'If the value was not found, Err will not be zero
        
            On Error GoTo 0  'clearing the error code
            
            Sheets(1).Activate   'go back to sheet 1
            
            r = ActiveCell.Row   'get a hold of current row index
            
            Range(r & ":" & r).Select  'select the whole row
            
            cnt = cnt + 1   'increment the counter
            
            Selection.Copy  'copy current selection
            
            Sheets(3).Activate  'go to sheet 3
            
            ActiveCell.PasteSpecial xlPasteAll  'Past the entire row to sheet 3
            
            ActiveCell.Offset(1, 0).Select  'go down one row to prepare for next row.
            
            
        End If
        Sheets(1).Activate   'return to sheet 1
        ActiveCell.Offset(1, 0).Select   'go to the next row
        
    Loop   'repeat
    Application.ScreenUpdating = True
    Application.CutCopyMode = False
    Sheets(3).Activate    'go to sheet 3 to examine findings
    
    
    MsgBox "I have found " & cnt & " rows that did not exist in sheet 2"
    
End Sub
Reply With Quote