How to line up match rows between various columns

cytochrome

New Member
Joined
Feb 8, 2017
Messages
18
Office Version
  1. 365
Platform
  1. Windows
Hi there,
Just wondering what is the best way to accomplish this.
I have 6 columns of data.

G1CA1
134​
G1CA1
134​
G1CA2
112.15​
G1CA2
110​
G2CA3
4264.2​
G3CA1
4264.2​
G4CA3
12.98​
G4CA3
12​
G5CA3
32.18​
G5CA4
32.18​
G6CA4
46.25​
G6CA4
46.25​
G7CA5
160.78​
G8CA5
1​
G9CA91
G10CA102

What's the best way to rearrange/sort them to have it look like this?

G1CA1
134​
G1CA1
134​
G1CA2
112.15​
G1CA2
110​
G2CA3
4264.2​
G3CA1
4264.2​
G4CA3
12.98​
G4CA3
12​
G5CA3
32.18​
G5CA4
32.18​
G6CA4
46.25​
G6CA4
46.25​
G7CA5
160.78​
G8CA5
1​
G9CA91
G10CA102

Basically the conditions to match up are A&B against D&E. If there is a VBA that can arrange that it would be amazing.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hello,
I am not sure VBA will work to sort your data as you wish. The variables to sort by are invalid, meaning, CA1-CA10 would sort like this;
CA1
CA10
CA2
CA3 and so on.
If you concatenate the values in A and B, and the values in D and E, and then sort each section (A-C and D-F) individually by the concatenated value, it will sort it improperly.

You could have VBA separate the values in A and B, and D and E, between characters and numbers, sort that (again sorting A-C and D-F individually, and then have the VBA concatenate the values back together after the sort is done. Not entirely sure if all that is plausible?
 
Upvote 0
So minus VBA possibility. Is there an easy way to arrange/sort that data?
 
Upvote 0
So minus VBA possibility. Is there an easy way to arrange/sort that data?
If you concatenate the values in A and B, and the values in D and E, and then sort each section (A-C and D-F) individually by the concatenated value, it will sort it improperly.
But this will get you close to where you want to be.
 
Upvote 0
You might want to try this with a few different scenarios, and it does assume the numeric suffix in columns A and D data is already in order, but it seems to work on the data in your sample. I won't say it will work with any combination, but it is a start.
VBA Code:
Option Explicit
Sub cytochrome()
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")   '<~~ Change to actual sheet name if needed
    Dim LRow As Long, i As Long, A As Long, B As Long, s As String
    LRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
   
    For i = LRow To 1 Step -1
        If ws.Cells(i, 1) <> "" Or ws.Cells(i, 4) <> "" Then
            If ws.Cells(i, 1) <> "" Then s = ws.Cells(i, 1): A = GetNum(s)
            If ws.Cells(i, 4) <> "" Then s = ws.Cells(i, 4): B = GetNum(s)
           
            If (A - B) < 0 And ws.Cells(i, 4) <> "" Then
                ws.Cells(i, 4).Resize(, 3).Insert xlDown
                ws.Cells(i + 1, 1).Resize(, 3).Insert xlDown
            End If
            If (A - B) > 0 And ws.Cells(i, 4) <> "" Then ws.Cells(i, 1).Resize(, 3).Insert xlDown
        End If
    Next i
End Sub

Function GetNum(s As String) As Long
    GetNum = Mid(s, 2, Len(s) - 1)
End Function

Before:
Book1
ABCDEF
1G1CA1134G1CA1134
2G1CA2112.15G1CA2110
3G2CA34264.2G3CA14264.2
4G4CA312.98G4CA312
5G5CA332.18G5CA432.18
6G6CA446.25G6CA446.25
7G7CA5160.78G8CA51
8G9CA91
9G10CA102
Sheet1


After:
Book1
ABCDEF
1G1CA1134G1CA1134
2G1CA2112.15G1CA2110
3G2CA34264.2
4G3CA14264.2
5G4CA312.98G4CA312
6G5CA332.18G5CA432.18
7G6CA446.25G6CA446.25
8G7CA5160.78
9G8CA51
10G9CA91
11G10CA102
Sheet1
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

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