Sorting and Aligning Rows

VBAEnjoi

New Member
Joined
Sep 30, 2018
Messages
33
I have data in n number of rows and 2 columns.
I want to sort data in rows so that the matching values of 2 columns are aligned in the same row.
The Original data set and Required outcome below:
Original Data set
RowColumn AColumn B
1Apple2RedApple1Red
2Apple1RedOrange5Red
3Apple1 RedOrange1Red
4Apple1YellowApple1Yellow
5Orange1BlackApple1Red
6Orange1WhiteApple1Red
7Orange5RedOrange1White

<tbody>
</tbody>

Required outcome

Column AColumn B
Apple1 RedApple1Red
Apple1RedApple1Red
Apple1Red
Apple1YellowApple1Yellow
Apple2Red
Orange1Black
Orange1WhiteOrange1White
Orange1Red
Orange5RedOrange5Red

<tbody>
</tbody>
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Your test data is treating text with space the same as text without space
(The second Apple1 Red in column A contains a space but is treated as if it does not in your example)
- so the VBA below includes a step to remove spaces in order to return required result
- if that is not what you want, then remove that step

Your sample data
Excel 2016 (Windows) 32 bit
A
B
1
Apple2RedApple1Red
2
Apple1RedOrange5Red
3
Apple1 RedOrange1Red
4
Apple1YellowApple1Yellow
5
Orange1BlackApple1Red
6
Orange1WhiteApple1Red
7
Orange5RedOrange1White
Sheet: Sheet1


Sheet copied to temporary sheet and ALL spaces removed

Excel 2016 (Windows) 32 bit
A
B
1
Apple2RedApple1Red
2
Apple1RedOrange5Red
3
Apple1RedOrange1Red
4
Apple1YellowApple1Yellow
5
Orange1BlackApple1Red
6
Orange1WhiteApple1Red
7
Orange5RedOrange1White
Sheet: Sheet1 (2)

ALL data (from both columns) is copied to column A and duplicated in column B
Excel 2016 (Windows) 32 bit
A
B
1
Apple1RedApple1Red
2
Apple1RedApple1Red
3
Apple1RedApple1Red
4
Apple1RedApple1Red
5
Apple1RedApple1Red
6
Apple1YellowApple1Yellow
7
Apple1YellowApple1Yellow
8
Apple2RedApple2Red
9
Orange1BlackOrange1Black
10
Orange1RedOrange1Red
11
Orange1WhiteOrange1White
12
Orange1WhiteOrange1White
13
Orange5RedOrange5Red
Sheet: DataDuplicated

Unwanted values removed from each column
Excel 2016 (Windows) 32 bit
A
B
1
Apple1RedApple1Red
2
Apple1RedApple1Red
3
Apple1Red
4
5
6
Apple1YellowApple1Yellow
7
8
Apple2Red
9
Orange1Black
10
Orange1Red
11
Orange1WhiteOrange1White
12
13
Orange5RedOrange5Red
Sheet: ValuesCleared

Resultant empty rows deleted
Excel 2016 (Windows) 32 bit
A
B
1
Apple1RedApple1Red
2
Apple1RedApple1Red
3
Apple1Red
4
Apple1YellowApple1Yellow
5
Apple2Red
6
Orange1Black
7
Orange1Red
8
Orange1WhiteOrange1White
9
Orange5RedOrange5Red
Sheet: Result

Code:
Sub SortData()
    Dim ws As Worksheet, sh As Worksheet
    Dim cel As Range, celA As Range, celB As Range, rngA As Range, rngB As Range, rngA2 As Range, rngB2 As Range, Del As Range
    Dim r As Long, txt As String
    
[I][COLOR=#006400]'add sheet and remove all spaces from text[/COLOR][/I]
    Sheets("Sheet1").Copy Before:=Sheets(1)
    Set sh = Sheets(1)
    Set rngA = sh.Range("A1").CurrentRegion.Resize(, 1)
    Set rngB = rngA.Offset(, 1)
    For Each cel In Union(rngA, rngB)
        cel = Replace(cel, " ", "")
    Next

[COLOR=#006400][I]'add sheet, copy all data (both columns A&B) to column A and sort[/I][/COLOR]
    Set ws = Sheets.Add
    rngA.Copy ws.Cells(1, 1)
    rngB.Copy ws.Cells(ws.Rows.Count, 1).End(xlUp)
    ws.Range("A:A").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlNo
[COLOR=#006400][I]'copy columnA to column B and remove items not required from each column[/I][/COLOR]
    ws.Range("A:A").Copy ws.Cells(1, 2)
    Set Del = ws.Cells(ws.Rows.Count, 1)
    For r = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row To 1 Step -1
        Set celA = ws.Cells(r, 1)
        Set celB = celA.Offset(, 1)
        Set rngA2 = ws.Cells(1, 1).Resize(r)
        Set rngB2 = rngA2.Offset(, 1)
        txt = celA
        If Occurs(rngA2, txt) > Occurs(rngA, txt) Then celA.ClearContents
        If Occurs(rngB2, txt) > Occurs(rngB, txt) Then celB.ClearContents
        If Len(celA) + Len(celB) = 0 Then Set Del = Union(Del, celA)
        txt = ""
    Next r
[COLOR=#006400][I]'delete blank rows and temporary sheet[/I][/COLOR]
    Del.EntireRow.Delete
    Application.DisplayAlerts = False
        sh.Delete
    Application.DisplayAlerts = True
End Sub

Private Function Occurs(xRange As Range, xText As String) As Long
    Occurs = WorksheetFunction.CountIf(xRange, xText)
End Function
 
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,982
Members
449,201
Latest member
Lunzwe73

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