Sorting 2 columns to match when slightly different

Keegan4123

New Member
Joined
Mar 30, 2022
Messages
29
Office Version
  1. 2016
Platform
  1. Windows
Have a daily data set I need to combine.
1st set contains all vehicles, 2nd set contains only vehicles used today, so far have been pasting them next to eachother, then manually sorting line by line so that the same units line up, and we can perform the calculations. Is there a way to easily sort the two ranges so that they match line by line. One would either have gaps (thats okay) or one would be out of sort (have top sorted, and unused units on the bottom (also okay)

Book1
ABCDEFGHIJKLMN
1Intial Data Set
2GPS SystemPRETRIP SYSTEM
3Unit NumberKMUnit NumberKM
4Unit 185,000Unit 185000
5Unit 286,000Unit 387000
6Unit 387,000Unit 488000
7Unit 488,000Unit 690000
8Unit 589,000Unit 791000
9Unit 690,000Unit 993000
10Unit 791,000
11Unit 892,000
12Unit 993,000
13
14
15Option 1Option 2
16
17GPS SystemPRETRIP SYSTEMGPS SystemPRETRIP SYSTEM
18Unit NumberKMUnit NumberKMUnit NumberKMUnit NumberKM
19Unit 185,000Unit 185000Unit 185,000Unit 185000
20Unit 387,000Unit 387000Unit 286,000
21Unit 488,000Unit 488000Unit 387,000Unit 387000
22Unit 690,000Unit 690000Unit 488,000Unit 488000
23Unit 791,000Unit 791000Unit 589,000
24Unit 993,000Unit 993000Unit 690,000Unit 690000
25Unit 286,000Unit 791,000Unit 791000
26Unit 589,000Unit 892,000
27Unit 993,000Unit 993,000Unit 993000
28
Sheet1
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi @Keegan4123, thanks for posting on the forum.


Try this macro to get option 2. The macro assumes, as your example does, that the data starts in cell A4 and you want the result starting in cell E4 onwards.
VBA Code:
Sub SortingColumns()
  Dim a As Variant, b As Variant, c As Variant
  Dim rng As Range, f As Range
  Dim i As Long, j As Long, k As Long
  
  Set rng = Range("A4", Range("A" & Rows.Count).End(3))
  a = Range("E4:F" & Range("E" & Rows.Count).End(3).Row).Value
  ReDim b(1 To rng.Rows.Count, 1 To 2)
  ReDim c(1 To rng.Rows.Count, 1 To 2)
  
  For i = 1 To UBound(a, 1)
    Set f = rng.Find(a(i, 1), , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then
      j = f.Row - 3
      b(j, 1) = a(i, 1)
      b(j, 2) = a(i, 2)
    Else
      k = k + 1
      c(k, 1) = a(i, 1)
      c(k, 2) = a(i, 2)
    End If
  Next
  
  Range("E4:F" & Rows.Count).ClearContents
  Range("E4").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  Range("E" & Rows.Count).End(3)(2).Resize(UBound(c, 1), UBound(c, 2)).Value = c
  
End Sub


--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
 
Upvote 0
Hi @Keegan4123, thanks for posting on the forum.


Try this macro to get option 2. The macro assumes, as your example does, that the data starts in cell A4 and you want the result starting in cell E4 onwards.
VBA Code:
Sub SortingColumns()
  Dim a As Variant, b As Variant, c As Variant
  Dim rng As Range, f As Range
  Dim i As Long, j As Long, k As Long
 
  Set rng = Range("A4", Range("A" & Rows.Count).End(3))
  a = Range("E4:F" & Range("E" & Rows.Count).End(3).Row).Value
  ReDim b(1 To rng.Rows.Count, 1 To 2)
  ReDim c(1 To rng.Rows.Count, 1 To 2)
 
  For i = 1 To UBound(a, 1)
    Set f = rng.Find(a(i, 1), , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then
      j = f.Row - 3
      b(j, 1) = a(i, 1)
      b(j, 2) = a(i, 2)
    Else
      k = k + 1
      c(k, 1) = a(i, 1)
      c(k, 2) = a(i, 2)
    End If
  Next
 
  Range("E4:F" & Rows.Count).ClearContents
  Range("E4").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  Range("E" & Rows.Count).End(3)(2).Resize(UBound(c, 1), UBound(c, 2)).Value = c
 
End Sub


--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
Thanks so much for your suggestion! That looks like it will work perfectly from my limited knowledge on macros, but I was able to find a bit more of an easy solution using a helper column and the formula =match(a4,E:E,false) then sorting the range by the result.
 
Upvote 0

Forum statistics

Threads
1,215,129
Messages
6,123,218
Members
449,091
Latest member
jeremy_bp001

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