match records on same row

JugglerJAF

Active Member
Joined
Feb 17, 2002
Messages
297
Office Version
  1. 365
Platform
  1. Windows
I have two columns of data (Columns A and B).

Some of the values are common to both lists.
Some values will appear on A but not on B.
Some values will appear on B but not on A.
Both lists should be of unique entries and will be sorted in ascending order.
The lists may be the same size, but will probably have different numbers of values.

I'm looking for a macro that will move the data so that where matches are found, they appear on the same line, and where there is no match, the list which does not contain a match will have a blank.

For example:
Excel Workbook
AB
1List 1List 2
2AlphaBravo
3BravoEcho
4CharlieFoxtrot
5DeltaGolf
6EchoHotel
7GolfIndia
8IndiaJuliet
9MikeKilo
10Papa*
11Romeo*
Sheet1


would become
Excel Workbook
HI
1List 1List 2
2Alpha*
3BravoBravo
4Charlie*
5Delta*
6EchoEcho
7*Foxtrot
8GolfGolf
9*Hotel
10IndiaIndia
11*Juliet
12*Kilo
13Mike*
14Papa*
15Romeo*
Sheet1


NB: Asterisks in the tables above denote blank cells.


I used to have some code that did this, but lost it when I changed jobs. I must have had it saved at my old place of work, but not backed up anywhere!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Assuming your data headers are in row 1 and your data starts in row 2, your original data in columns A and B and you wish your adjusted data to be in columns H and I, try:
Code:
Sub MySort ()
Range("A:B").copy
Range("H1").pastespecial paste:=xlpastevalues
Dim i as Long
i = 2
Do until IsEmpty(Range("H" & i)) AND IsEmpty(Range("I" & i))
If Range("H" & i) < Range("I" & i) Then
  Range("I" & i).insert Shift:=xlDown
Else
  If Range("H" & i) > Range("I" & i) Then _
    Range("H" & i).insert shift:=xldown
End If
i = i + 1
Loop
End Sub
This hasn't been tested and trying to do this purely from memory/sight so back up your work first before trying!
 
Upvote 0
That works a treat. Thanks.

I've had to add in a step to ensure that each data set contains the same final value as the code as written kept looping if the final value in one list wasn't in the other, but I've just added some code to add the value "ZZZZZZZ" to the end of each set of values in the two lists, apply your code and then delete the "ZZZZZZZ" value.
 
Upvote 0

Forum statistics

Threads
1,214,787
Messages
6,121,569
Members
449,038
Latest member
Guest1337

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