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
 
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? -
Yes this is absolutely correct.

Let me explain in another way. Im comparing two items, one of which has records setup in all warehouse cities (Cols A and B) and the second item which are setup in a lesser no of warehouses (Cols C and D). What Im trying to get with the VBA is prepared data that I can upload into my system. Therefore my final goal is to get the data from Col C in Col E (exactly the same) and then the warehouse cities of Col D that are missing in Col B in Col F.

Book7
ABCDEF
1item 1Austinitem 2Austinitem 2Chicago
2item 1Bostonitem 2Bostonitem 2Houston
3item 1Chicagoitem 2Dallasitem 2Portland
4item 1Dallasitem 2New York
5item 1Houstonitem 2Seattle
6item 1New York
7item 1Portland
8item 1Seattle
Sheet1


Hope this is clearer. Thanks again.
 
Upvote 0

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
You mentioned formula is cumbersome but would this work for your purposes?
Book1
ABCDEF
1AAustinBAustinBChicago
2ABostonBBostonBHouston
3AChicagoBDallasBPortland
4ADallasBNew York
5AHoustonBSeattle
6ANew York
7APortland
8ASeattle
Sheet6
Cell Formulas
RangeFormula
E1:E3E1=TAKE(C1:C5,COUNTA(F1#))
F1:F3F1=UNIQUE(VSTACK(B1:B8,D1:D5),,1)
Dynamic array formulas.
 
Upvote 0
OK, this update to the code should do what you want:
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 value from column B to column F
            Cells(r, "B").Copy Cells(x, "F")
'           Copy first value in column C to column E
            Cells(1, "C").Copy Cells(x, "E")
        End If
    Next r
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Solution
Yes. (y) This worked. Thanks a lot for your help and effort. Appreciate it very much.
 
Upvote 0
You are welcome.
Glad we were able to help!

Note that if you are using Excel 365, Cubist did offer a formula solution that is not too cumbersome.
 
Upvote 0
You mentioned formula is cumbersome but would this work for your purposes?
Book1
ABCDEF
1AAustinBAustinBChicago
2ABostonBBostonBHouston
3AChicagoBDallasBPortland
4ADallasBNew York
5AHoustonBSeattle
6ANew York
7APortland
8ASeattle
Sheet6
Cell Formulas
RangeFormula
E1:E3E1=TAKE(C1:C5,COUNTA(F1#))
F1:F3F1=UNIQUE(VSTACK(B1:B8,D1:D5),,1)
Dynamic array formulas.
Hi Cubist. yes this also worked great. I'll use this as well too. Thanks
 
Upvote 0

Forum statistics

Threads
1,223,841
Messages
6,174,976
Members
452,596
Latest member
Anabaric

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