confused sort range based on names in column

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hi experts
I try sorting data based on column B
this is what I want
a.xlsx
ABCDEFG
71alla127422/05/20061549
82alla226601/07/20061530
93alla327520/08/200615111
104alla427608/02/200714723
115alla527120/01/200714811
126alla626613/05/200615418
137alla727118/06/200615313
148alla826929/09/20051602
159alla927602/07/200615229
1610alla1026901/07/20061530
ss
Cell Formulas
RangeFormula
E7:E16E7=IF($D7="","",DATEDIF($D7,DATE(YEAR(NOW()),10,1),"y"))
F7:F16F7=IF($D7="","",DATEDIF($D7,DATE(YEAR(NOW()),10,1),"ym"))
G7:G16G7=IF($D7="","",DATEDIF($D7,DATE(YEAR(NOW()),10,1),"md"))

but suddenly this is what I got

a.xlsx
ABCDEFG
71alla127422/05/20061549
82alla1026901/07/20061530
93alla1127524/07/20061527
104alla1227601/06/20061540
115alla1327125/09/20051606
126alla1427201/08/20061520
137alla1526928/09/20061503
148alla1627002/05/200615429
159alla1727127/04/20061554
1610alla1827114/01/200615817
1711alla226601/07/20061530
1812alla327520/08/200615111
1913alla427608/02/200714723
2014alla527120/01/200714811
2115alla626613/05/200615418
2216alla727118/06/200615313
ss
Cell Formulas
RangeFormula
E7:E22E7=IF($D7="","",DATEDIF($D7,DATE(YEAR(NOW()),10,1),"y"))
F7:F22F7=IF($D7="","",DATEDIF($D7,DATE(YEAR(NOW()),10,1),"ym"))
G7:G22G7=IF($D7="","",DATEDIF($D7,DATE(YEAR(NOW()),10,1),"md"))

how should fix,please?
 
Is there anything currently in columns H and I, or are those empty and OK to use those for our "helper" columns?
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
If columns H and I are available, here is an update to your current code that should do what you want:
VBA Code:
Private Sub WorkSheet_Change(ByVal Target As Range)
    
    Dim lastrow As Long

'   Only run if data updated under cell B6
    If Target.Column = 2 And Target.Row > 6 Then
'       Temporarily disable events
        Application.EnableEvents = False
'       Find last row
        lastrow = Cells(Rows.Count, 2).End(xlUp).Row
'       Populate columns H and I with formulas
        Range("H7:H" & lastrow).FormulaR1C1 = _
            "=IF(ISERROR(RIGHT(RC[-6],2)+0),LEFT(RC[-6],LEN(RC[-6])-1),LEFT(RC[-6],LEN(RC[-6])-2))"
        Range("I7:I" & lastrow).FormulaR1C1 = _
            "=IF(ISERROR(RIGHT(RC[-7],2)+0),RIGHT(RC[-7],1),RIGHT(RC[-7],2))+0"
'       Sort data based on helper columns
        ActiveSheet.Sort.SortFields.Clear
        Range("B7:I" & lastrow).Sort key1:=Range("H7:H" & lastrow), order1:=xlAscending, _
            key2:=Range("I7:I" & lastrow), order2:=xlAscending, Header:=xlNo
'       Make sure columns H and I are hidden
        Range("H7:I7").EntireColumn.Hidden = True
'       Re-enable events
        Application.EnableEvents = True
    End If
    
End Sub
 
Upvote 0
Solution
You are welcome!
I am glad it works for you.

I added lots of comments to my code so you can follow along and see what each step is doing.
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

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