confused sort range based on names in column

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,439
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?
 
I understand your question/problem and understand what your desired result is.
I need you to tell me which solution option you would like us to help you incorporate!

At the end of my last post, I said:
So I think you really just have two options here:
1. Enlist the use of helper columns for sorting purposes (which you can hide, by the way).
2. Fix/update the data, as I described in my previous post.

Note that you can use VBA in conjunction with either of these solutions. You could even have VBA add the helper columns, complete the sort, then delete the helper columns after the sort if finished.
The choice is yours.
So which one would you like us to pursue?
Note that if we go with option 2, all your values like: "ALI1", "AL2", "AL3" will be permanently changed to "AL01", "AL02", "AL03".
Are you OK with that?

If not, then I think enlisting the use of helpers columns is the only way to go. We can hide them, or even use VBA to delete them after the sort, if you like.

I just need you to tell me which option you would like to go with.
 
Upvote 0

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
If not, then I think enlisting the use of helpers columns is the only way to go. We can hide them, or even use VBA to delete them after the sort, if you like.
yes
 
Upvote 0
I would hide because if i add a new name the code doesn't work for helper column when delete . am I right or wrong?
 
Upvote 0
OK, let's start with a non-VBA approach with helper columns, and work from there.

So, we want to set up two helper columns let's use H and I.

Since our first row of data is in cell B7, place this formula in cell H7 and copy down for all rows:
Excel Formula:
=IF(ISERROR(RIGHT(B7,2)+0),LEFT(B7,LEN(B7)-1),LEFT(B7,LEN(B7)-2))

Likewise, place this formula in cell I7 and copy down for all rows:
Excel Formula:
=IF(ISERROR(RIGHT(B7,2)+0),RIGHT(B7,1),RIGHT(B7,2))+0

Now, you can sort your data, using BOTH columns H and I in your sort (H is the primary sort column and column I is the secondary one).
One you are done sorting, you can hide both columns H and I.
 
Upvote 0
can you show me how the code should be after add the formulas,please?
 
Upvote 0
I am not sure what you mean. Do you mean what the sort looks like?
It looks something like this:

1636562564551.png


Note that even when columns H and I are hidden, you can still sort by it.
 
Upvote 0
I thought your formula support my code in OP. do you mean sort manually?
 
Upvote 0
I thought your formula support my code in OP. do you mean sort manually?
I don't think we are on the same page here. You don't have any "code" in your OP, only Excel formulas ("code" is usually sort for "VBA code").
Are you saying that you want VBA code to automatically add these helper columns and do the sorting for you?
 
Upvote 0
I don't think we are on the same page here. You don't have any "code" in your OP
Oh MGH ! my apologies :eek:
this is my code automatically when add the names in column B should sort
VBA Code:
Option Explicit

Private Sub WorkSheet_Change(ByVal Target As Range)
If Target.Column = 2 Then
Dim lastrow As Long
lastrow = Cells(Rows.Count, 2).End(xlUp).Row
Range("b7:g" & lastrow).Sort key1:=Range("b7:b" & lastrow), order1:=xlAscending, Header:=xlNo
End If
End Sub

Are you saying that you want VBA code to automatically add these helper columns and do the sorting for you?
yes
 
Upvote 0

Forum statistics

Threads
1,216,523
Messages
6,131,171
Members
449,627
Latest member
ChrisNoMates

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