Having RANK work with VB to convert 1 to 1st

slam

Well-known Member
Joined
Sep 16, 2002
Messages
921
Office Version
  1. 365
  2. 2019
I had this VB code that worked great when I entered values as 1, 2, 3 etc.

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Cells.Count > 1 Then Exit Sub
Application.EnableEvents = False
Select Case Target.Formula
Case 1: Target.Value = Target.Value & "st"
Case 2: Target.Value = Target.Value & "nd"
Case 3: Target.Value = Target.Value & "rd"
Case 4 To 16: Target.Value = Target.Value & "th"
End Select
Application.EnableEvents = True

End Sub

However, I am now slightly changing the functionality of that column so that the values are automatically entered based on the value in the adjacent column using a RANK formula.

I have this formula

=IF(ISBLANK(E2),"",RANK(E2,$E$2:$E$17,1))

Unfortunately, the values are no longer converted to 1st, 2nd, 3rd etc. Is there any way to do this? - make the formula and VB work together?

Thanks
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Try Target.Value instead of Target.Formula.

As follows?



Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Cells.Count > 1 Then Exit Sub
Application.EnableEvents = False
Select Case Target.Value
Case 1: Target.Value = Target.Value & "st"
Case 2: Target.Value = Target.Value & "nd"
Case 3: Target.Value = Target.Value & "rd"
Case 4 To 16: Target.Value = Target.Value & "th"
End Select
Application.EnableEvents = True

End Sub




No luck unfortunately. Still leaves the result as 1 instead of 1st....
 
Upvote 0
Sorry, I didn't think that through before posting (but generally you should test .Value not .Formula.

How about doing away with the VBA and modifying the formula?

=IF(ISBLANK(E2),"",RANK(E2,$E$2:$E$17,1)&LOOKUP(RANK(E2,$E$2:$E$17,1),{1,2,3,4},{"st","nd","rd","th"}))
 
Upvote 0
Sorry, I didn't think that through before posting (but generally you should test .Value not .Formula.

How about doing away with the VBA and modifying the formula?

=IF(ISBLANK(E2),"",RANK(E2,$E$2:$E$17,1)&LOOKUP(RANK(E2,$E$2:$E$17,1),{1,2,3,4},{"st","nd","rd","th"}))

That is excellent. I'd much rather have a formula than VB, and that works great. I trust there is some build in excel wizardry that allows it to know that 5 through 16 also ends in "th"?
 
Upvote 0
I had this VB code that worked great when I entered values as 1, 2, 3 etc.

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Cells.Count > 1 Then Exit Sub
Application.EnableEvents = False
Select Case Target.Formula
Case 1: Target.Value = Target.Value & "st"
Case 2: Target.Value = Target.Value & "nd"
Case 3: Target.Value = Target.Value & "rd"
Case 4 To 16: Target.Value = Target.Value & "th"
End Select
Application.EnableEvents = True

End Sub

However, I am now slightly changing the functionality of that column so that the values are automatically entered based on the value in the adjacent column using a RANK formula.

I have this formula

=IF(ISBLANK(E2),"",RANK(E2,$E$2:$E$17,1))

Unfortunately, the values are no longer converted to 1st, 2nd, 3rd etc. Is there any way to do this? - make the formula and VB work together?

Thanks
You can combine the rank and ordinal in the same formula.

Book1
EF
1ValueRank
2968th
3102nd
4__
5545th
6816th
721st
8887th
9344th
10223rd
Sheet1

This formula entered in F2 and copied down:

=IF(E2="","",RANK(E2,E$2:E$10,1)&MID("thstndrdth",MIN(9,2*RIGHT(RANK(E2,E$2:E$10,1))*(MOD(RANK(E2,E$2:E$10,1)-11,100)>2)+1),2))
 
Upvote 0
Basically 4 and above will return th. That's OK as you are only going up to 16. If you were going up to 21 then the formula would need amending (and potentially get messy).
 
Upvote 0

Forum statistics

Threads
1,224,564
Messages
6,179,547
Members
452,925
Latest member
duyvmex

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