Sorting VBA

johnko

Board Regular
Joined
Aug 18, 2016
Messages
71
Hello there! I need some help in sorting out data based on same one cell data value but still separate them apart because of different values from other cells, I will upload one copy of excel sheet and share the link here below for you guys to have a better understanding, inside the spreadsheet there is a "before" and "after" sheet name which indicates raw data and the results I want respectively. Thank you.

https://app.box.com/s/kc4pyx0nytcic0z3cx10rh85o45hey2r


eg. Before

A
G
CLIENT
CONTRACT
John
Apple
John
Apple
John
Apple
Aaron
Durian
David
Banana
John
Orange
John
Orange
John
Orange
David
Honey
David
Honey

<tbody>
</tbody>


Results:
A
G
John
Apple
John
Apple
John
Apple
John
Orange
John
Orange
John
Orange
Aaron
Durian
David
Banana
David
Honey
David
Honey

<tbody>
</tbody>


Noted that I want them to group together based on same cell value on "A" which is the Client's name but still want to leave a gap between them because of different cell value on "G" which is the contract name. Hope to hear from you guys soon thanks!
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Give this a try

Code:
Sub t()
With ActiveSheet
    lr = .Cells(Rows.Count, 1).End(xlUp).Row
    .UsedRange.Sort .Range("A2"), xlAscending, , .Range("G2"), xlAscending, , , xlYes
        For i = lr To 3 Step -1
            If .Cells(i, 1).Value <> .Cells(i - 1, 1).Value Then
                Rows(i).Insert
            End If
            If .Cells(i, 7).Value <> .Cells(i - 1, 7).Value And .Cells(i, 1) <> "" Then
                Rows(i).Insert
            End If
        Next
End With
End Sub
 
Upvote 0
Give this a try

Code:
Sub t()
With ActiveSheet
    lr = .Cells(Rows.Count, 1).End(xlUp).Row
    .UsedRange.Sort .Range("A2"), xlAscending, , .Range("G2"), xlAscending, , , xlYes
        For i = lr To 3 Step -1
            If .Cells(i, 1).Value <> .Cells(i - 1, 1).Value Then
                Rows(i).Insert
            End If
            If .Cells(i, 7).Value <> .Cells(i - 1, 7).Value And .Cells(i, 1) <> "" Then
                Rows(i).Insert
            End If
        Next
End With
End Sub

This works like a charm! Thank you!
 
Upvote 0

Forum statistics

Threads
1,216,741
Messages
6,132,448
Members
449,728
Latest member
teodora bocarski

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