Compare Two Columns, Return Missing Values in Separate Columns

censo

Board Regular
Joined
Dec 25, 2015
Messages
161
Office Version
  1. 2013
Platform
  1. Windows
Greetings,

I have 2 columns that are supposed to have equal values. I need assistance creating a macro that will compare both columns A:B and return values that are missing in columns C:D

If both columns match, do nothing (return no values)

Below is a visual example of the desired outcome

new_thread_post.xlsx
ABCD
1SKU SOURCE 1SKU SOURCE 2MISSING FROM COLUMN AMISSING FROM COLUMN B
2ABC-123ABC-123ABC-123-003ABC-123-001
3ABC-123-001ABC-123-002ABC-123-007ABC-123-005
4ABC-123-002ABC-123-003ABC-123-010
5ABC-123-004ABC-123-004
6ABC-123-005ABC-123-006
7ABC-123-006ABC-123-007
8ABC-123-008ABC-123-008
9ABC-123-009ABC-123-009
10ABC-123-010
Sheet1
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Here is one way:
VBA Code:
Sub PerformMatch()

    Dim lrA As Long
    Dim lrB As Long
    Dim rngA As Range
    Dim rngB As Range
    Dim cell As Range
    
    Application.ScreenUpdating = False
    
'   Find last row with data in column A
    lrA = Cells(Rows.Count, "A").End(xlUp).Row
'   Find last row with data in column B
    lrB = Cells(Rows.Count, "B").End(xlUp).Row
    
'   Set data ranges
    Set rngA = Range("A2:A" & lrA)
    Set rngB = Range("B2:B" & lrB)
    
'   Loop through all rows in column B
    For Each cell In rngB
'       Search for value in column A
        If Application.WorksheetFunction.CountIf(rngA, cell.Value) = 0 Then
'           Copy value to column D
            Cells(Rows.Count, "C").End(xlUp).Offset(1, 0) = cell.Value
        End If
    Next cell

'   Loop through all rows in column A
    For Each cell In rngA
'       Search for value in column B
        If Application.WorksheetFunction.CountIf(rngB, cell.Value) = 0 Then
'           Copy value to column D
            Cells(Rows.Count, "D").End(xlUp).Offset(1, 0) = cell.Value
        End If
    Next cell
     
    Application.ScreenUpdating = True
    
    MsgBox "Macro complete!"
    
End Sub
 
Upvote 1
Solution
Worked beautifully, and complete with notation so I can understand what's going on under the hood.

Thank you SO much!
 
Upvote 0
You are welcome.
Glad I was able to help!
:)
 
Upvote 0
Here is one way:
VBA Code:
Sub PerformMatch()

    Dim lrA As Long
    Dim lrB As Long
    Dim rngA As Range
    Dim rngB As Range
    Dim cell As Range
  
    Application.ScreenUpdating = False
  
'   Find last row with data in column A
    lrA = Cells(Rows.Count, "A").End(xlUp).Row
'   Find last row with data in column B
    lrB = Cells(Rows.Count, "B").End(xlUp).Row
  
'   Set data ranges
    Set rngA = Range("A2:A" & lrA)
    Set rngB = Range("B2:B" & lrB)
  
'   Loop through all rows in column B
    For Each cell In rngB
'       Search for value in column A
        If Application.WorksheetFunction.CountIf(rngA, cell.Value) = 0 Then
'           Copy value to column D
            Cells(Rows.Count, "C").End(xlUp).Offset(1, 0) = cell.Value
        End If
    Next cell

'   Loop through all rows in column A
    For Each cell In rngA
'       Search for value in column B
        If Application.WorksheetFunction.CountIf(rngB, cell.Value) = 0 Then
'           Copy value to column D
            Cells(Rows.Count, "D").End(xlUp).Offset(1, 0) = cell.Value
        End If
    Next cell
   
    Application.ScreenUpdating = True
  
    MsgBox "Macro complete!"
  
End Sub

Thanks for the code @Joe4

Is there any way to make the code to copy missing value from column A to below last row of column A instead of to new column C,
same as for column B, copy missing value from column B to below last row of column B instead of to new column D.
 
Upvote 0
Thanks for the code @Joe4

Is there any way to make the code to copy missing value from column A to below last row of column A instead of to new column C,
same as for column B, copy missing value from column B to below last row of column B instead of to new column D.
I would leave the code as-is, and then just add code that move the values from columns C and D to the bottom of A and B afterwards, so as not to interfere with the comparisons for the initial run, i.e.
VBA Code:
Sub PerformMatch()

    Dim lrA As Long
    Dim lrB As Long
    Dim rngA As Range
    Dim rngB As Range
    Dim cell As Range
    Dim lrC As Long
    Dim lrD As Long
    
    Application.ScreenUpdating = False
    
'   Find last row with data in column A
    lrA = Cells(Rows.Count, "A").End(xlUp).Row
'   Find last row with data in column B
    lrB = Cells(Rows.Count, "B").End(xlUp).Row
    
'   Set data ranges
    Set rngA = Range("A2:A" & lrA)
    Set rngB = Range("B2:B" & lrB)
    
'   Loop through all rows in column B
    For Each cell In rngB
'       Search for value in column A
        If Application.WorksheetFunction.CountIf(rngA, cell.Value) = 0 Then
'           Copy value to column D
            Cells(Rows.Count, "C").End(xlUp).Offset(1, 0) = cell.Value
        End If
    Next cell

'   Loop through all rows in column A
    For Each cell In rngA
'       Search for value in column B
        If Application.WorksheetFunction.CountIf(rngB, cell.Value) = 0 Then
'           Copy value to column D
            Cells(Rows.Count, "D").End(xlUp).Offset(1, 0) = cell.Value
        End If
    Next cell
    
'   Find last rows in columns C and D with data
    lrC = Cells(Rows.Count, "C").End(xlUp).Row
    lrD = Cells(Rows.Count, "D").End(xlUp).Row
    
'   Cut/paste values to bottom of columns A and B
    If lrC > 1 Then Range("C2:C" & lrC).Cut Range("A" & lrA + 1)
    If lrD > 1 Then Range("D2:D" & lrD).Cut Range("B" & lrB + 1)
     
    Application.ScreenUpdating = True
    
    MsgBox "Macro complete!"
    
End Sub
 
Upvote 1

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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