Help With Custom Sort

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,748
Office Version
  1. 365
Platform
  1. Windows
I have a sheet like below. There are 2 rows that have matching data in column C. I need a sort that will sort every set of rows that contain both Without Housing and With Housing to the top like the result.

1654082798113.png


As you can see only the 3 bottom sets of rows have both Without Housing and With Housing in column AJ so whenever there are any like that I would like them sorted to the top like below?

1654082907748.png


Thanks
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Columns D and E are my answer. Columns G-J are just the steps to make it a bit easier to follow.

MrExcelPlayground9-1 (version 1).xlsb
ABCDEFGHIJ
1DataSortedSteps
2aawccwoaa1ccwo
3aawccwbb1ccw
4bbwoddwocc0ddwo
5bbwoddwdd0ddw
6ccwoeewoee0eewo
7ccweeweew
8ddwoaawaaw
9ddwaawaaw
10eewobbwobbwo
11eewbbwobbwo
Sheet11
Cell Formulas
RangeFormula
D2:E11D2=SORTBY($A$2:$B$11,XLOOKUP($A$2:$A$11,UNIQUE(A2:A11),(1-MOD(COUNTIFS($A$2:$A$11,UNIQUE(A2:A11),$B$2:$B$11,"wo"),2)),"",0))
G2:G6G2=UNIQUE(A2:A11)
H2:H6H2=(1-MOD(COUNTIFS(A2:A11,G2#,B2:B11,"wo"),2))
I2:J11I2=SORTBY($A$2:$B$11,XLOOKUP($A$2:$A$11,G2#,H2#,"",0))
Dynamic array formulas.
 
Upvote 0
Thanks but is there a macro that could be done? Firstly the amount of rows will vary each time so obviously the range will, and I have columns with data up until about BZ so helper columns will prove awkward. The criteria really are columns C and AJ. I just put that small example for clarity.
 
Upvote 0
Even if the rows were coloured that contained both then that would help as I could do a sort by colour.
 
Upvote 0
Columns D and E are my answer. Columns G-J are just the steps to make it a bit easier to follow.

MrExcelPlayground9-1 (version 1).xlsb
ABCDEFGHIJ
1DataSortedSteps
2aawccwoaa1ccwo
3aawccwbb1ccw
4bbwoddwocc0ddwo
5bbwoddwdd0ddw
6ccwoeewoee0eewo
7ccweeweew
8ddwoaawaaw
9ddwaawaaw
10eewobbwobbwo
11eewbbwobbwo
Sheet11
Cell Formulas
RangeFormula
D2:E11D2=SORTBY($A$2:$B$11,XLOOKUP($A$2:$A$11,UNIQUE(A2:A11),(1-MOD(COUNTIFS($A$2:$A$11,UNIQUE(A2:A11),$B$2:$B$11,"wo"),2)),"",0))
G2:G6G2=UNIQUE(A2:A11)
H2:H6H2=(1-MOD(COUNTIFS(A2:A11,G2#,B2:B11,"wo"),2))
I2:J11I2=SORTBY($A$2:$B$11,XLOOKUP($A$2:$A$11,G2#,H2#,"",0))
Dynamic array formulas.
As I am getting no other help how would I amend this to accommodate all the other columns please?
 
Upvote 0
Assuming your table to sort has headers in row 1 and data starts from row 2
Assuming that data are paired (if a pair exists)

Find a free column at the right of the last column, for example column AL

Set in AL2 this formula:
Code:
=IF(OR(AND(C2=C3,AJ2<>AJ3),AND(C2=C1,AJ2<>AJ1)),"A-"&UPPER(C2)&REPT(" ",30-LEN(C2))&UPPER(AJ2)&REPT(CHAR(122),30-LEN(AJ2)),"Z-"&UPPER(C2)&REPT(" ",30-LEN(C2))&UPPER(AJ2)&REPT(CHAR(122),30-LEN(AJ2)))

Copy in a standard module of your vba project the following code:
VBA Code:
Sub SortWnWo()
Dim ZForm As String, SortCol As String, SortH As Long
'
SortCol = "A:AL"            '<<< The columns to sort, including the Helper column
Sheets("TheSheet").Select   '<<< The sheet with the data
'
ZForm = Range(SortCol).Cells(2, Range(SortCol).Columns.Count).Address
SortH = Range(SortCol).Resize(, Range(SortCol).Columns.Count - 1).Find(What:="*", After:=Range(SortCol).Range("A1"), _
    SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row - 1
'Clear current Formulas and re-set them:
Range(ZForm).Offset(1, 0).Resize(Rows.Count - 3, 1).ClearContents
Range(ZForm).Copy Range(ZForm).Offset(1, 0).Resize(SortH - 1, 1)
'Start Sort:
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add Key:=Range(ZForm).EntireColumn, _
  SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveSheet.Sort
    .SetRange Range(SortCol)
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
'Range(ZForm).Offset(1, 0).Resize(SortH - 1, 1).ClearContents       '** Remove extra formulas
End Sub
Adapt the two lines marked <<< to your situation

Then run Sub SortWnWo

Column AL will be used to sort the table; you can examine the content of this column, that have been set via the formula to better understand the approach used. If you prefer, the line marked ** can be "uncommented" (remove the initial single quotation mark) and the table will be cleared, except the formula in row 2.
 
Upvote 0
Assuming your table to sort has headers in row 1 and data starts from row 2
Assuming that data are paired (if a pair exists)

Find a free column at the right of the last column, for example column AL

Set in AL2 this formula:
Code:
=IF(OR(AND(C2=C3,AJ2<>AJ3),AND(C2=C1,AJ2<>AJ1)),"A-"&UPPER(C2)&REPT(" ",30-LEN(C2))&UPPER(AJ2)&REPT(CHAR(122),30-LEN(AJ2)),"Z-"&UPPER(C2)&REPT(" ",30-LEN(C2))&UPPER(AJ2)&REPT(CHAR(122),30-LEN(AJ2)))

Copy in a standard module of your vba project the following code:
VBA Code:
Sub SortWnWo()
Dim ZForm As String, SortCol As String, SortH As Long
'
SortCol = "A:AL"            '<<< The columns to sort, including the Helper column
Sheets("TheSheet").Select   '<<< The sheet with the data
'
ZForm = Range(SortCol).Cells(2, Range(SortCol).Columns.Count).Address
SortH = Range(SortCol).Resize(, Range(SortCol).Columns.Count - 1).Find(What:="*", After:=Range(SortCol).Range("A1"), _
    SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row - 1
'Clear current Formulas and re-set them:
Range(ZForm).Offset(1, 0).Resize(Rows.Count - 3, 1).ClearContents
Range(ZForm).Copy Range(ZForm).Offset(1, 0).Resize(SortH - 1, 1)
'Start Sort:
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add Key:=Range(ZForm).EntireColumn, _
  SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveSheet.Sort
    .SetRange Range(SortCol)
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
'Range(ZForm).Offset(1, 0).Resize(SortH - 1, 1).ClearContents       '** Remove extra formulas
End Sub
Adapt the two lines marked <<< to your situation

Then run Sub SortWnWo

Column AL will be used to sort the table; you can examine the content of this column, that have been set via the formula to better understand the approach used. If you prefer, the line marked ** can be "uncommented" (remove the initial single quotation mark) and the table will be cleared, except the formula in row 2.
Thank you that seemed to work. So if I want to save in my personal macro workbook I will just have to change helper column each time?
 
Upvote 0
You have to make sure that SortCol = "A:AL" reflects your situation, beeing the last column (AL, in the example) the column that in row 2 has the formula.
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,725
Members
448,987
Latest member
marion_davis

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