VBA to compare two pairs of columns and return missing values

sncb

Board Regular
Joined
Mar 17, 2011
Messages
168
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Good Day,

I have data that looks like this and would like to use VBA to obtain results faster. Essentially Im comparing pairs of columns A/B with C/D and getting the missing values in E/F

Data starts in A1 and lies in Sheet1

Right now I'm managing with excel formulas but the repetition is cumbersome so thinking that VBA might be a better way forward. Thanks for any help with this.

Book3
ABCDEF
1AAustinBAustinBChicago
2ABostonBBostonBHouston
3AChicagoBDallasBPortland
4ADallasBNew York
5AHoustonBSeattle
6ANew York
7APortland
8ASeattle
Sheet1
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Try this VBA code:
VBA Code:
Sub MyUnmatchMacro()

    Dim lr As Long, r As Long
    Dim x As Long
    
    Application.ScreenUpdating = False
    
'   Find last row in column B with data
    lr = Cells(Rows.Count, "B").End(xlUp).Row
    
'   Loop through all rows in column B
    For r = 1 To lr
'       See if value in column B is not found in column D
        If Application.WorksheetFunction.CountIf(Range("D:D"), Cells(r, "B").Value) = 0 Then
'           Increment unmatch counter
            x = x + 1
'           Copy values to column E and F
            Range(Cells(r, "A"), Cells(r, "B")).Copy Cells(x, "E")
        End If
    Next r
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Thanks Joe,

However in column E, Im getting the values from Col A when the expected result should be data from Col C. The data in Col F is correct and so are number of records.
 
Upvote 0
Thanks Joe,

However in column E, Im getting the values from Col A when the expected result should be data from Col C. The data in Col F is correct and so are number of records.
I am confused by your description then.
You said:
Essentially Im comparing pairs of columns A/B with C/D and getting the missing values in E/F

It looked to me like you wanted to match column B to column D, and then return the unmatched values from A/B to E/F.
If that is not the case, what is the purpose of column A?
Please explain exactly how you want the match to work, and where the values in columns E and F are coming from.
(Note that if you are matching BOTH columns A and B to C and D, you have NO matches in your example, and all records from A & B would be returned in E & F!)
 
Upvote 0
Hi Joe,

Thanks for your reply. Yes maybe I should have framed my question better. What Im trying to get in columns E and F is the combination of data in Col C with the values missing in D in comparison to col B. So in my example Chicago, Houston, Portland are missing setup with the data in Col C.

I agree there's no need of Col A from the perspective of the code itself but i just wanted to show how my data is before I run the code.
 
Upvote 0
Still a little unclear.
I can see that the values in column B which are not found in column D are being put in column F.
But where is the value in column E coming from?
How are columns A and C being used in this at all?

Perhaps you have oversimplified the problem/data to the point where we cannot see the relation, and it isn't making much sense.
 
Upvote 0
Hi Joe, Data in Col E is simply the data from Col C. Col A is not needed at all for the purpose of the code but I was just displaying my existing data.

I'm just trying to have prepared data in Cols E and F using the VBA output for an upload.
 
Upvote 0
If we are returning the UNMATCHED records, how do we know what value from column C to put in column E?
If it was matched records, it would be obvious. But since they are unmatched, how do we know where to pull the value from?
Or will all the records in column C be exactly the same for every single record, so it doesn't matter which record from column C we grab?

Or, is it that column C is really related to column B and NOT column D.
So when we find a value from column B that is not in column D, we grab the values from column B and C from that row, flip them around, and put them in columns E and F?
 
Upvote 0
Actually, it does not look like my last thought is correct, as there are values in B6:B8 but not in C6:C8.
So for the example of Portland, that you show in column E, how do you know that a "B" should be in column F?
Where does that come from for this Portland record?
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,161
Members
453,021
Latest member
Justyna P

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