Multiple Apps

Add The Correct Suffix To Make Any Number Ordinal

Ease of Use

Easy

Version tested with

2003 

Submitted by:

CreganTur

Description:

This code will evaluate any number and determine the correct suffix needed to make the number ordinal. 

Discussion:

Suppose you need a number to appear ordinal (1st, 2nd, 13th, 33rd, 94th, etc), but all of your numbers lack the needed suffix. Instead of writing it in by hand, you can use this simple Function to add the correct suffix to any number to make it ordinal. 

Code:

instructions for use

			

Public Function numSuffix(num As Long) 'suffix for numbers with last digit of 1 If Right(num, 2) = 11 Then numSuffix = num & "th" ElseIf CInt(Right(CStr(num), 1)) = 1 Then numSuffix = num & "st" 'suffix for numbers with last digit of 2 ElseIf Right(num, 2) = 12 Then numSuffix = num & "th" ElseIf CInt(Right(CStr(num), 1)) = 2 Then numSuffix = num & "nd" 'suffix for numbers with last digit of 3 ElseIf Right(num, 2) = 13 Then numSuffix = num & "th" ElseIf CInt(Right(CStr(num), 1)) = 3 Then numSuffix = num & "rd" 'suffix for all other numbers Else numSuffix = num & "th" End If End Function

How to use:

  1. Create a new Module
  2. Copy the code above
  3. Paste the code into your new module
  4. Click File
  5. Click Save
  6. When Prompted, create a new name to save your module as
 

Test the code:

  1. In your Module's Immediate Window Type ?numSuffix(x)
  2. Replace the x with any number
  3. An ordinal version of the number will be printed in the Immediate Window
 

Sample File:

Number Suffix.zip 12.01KB 

Approved by mdmackillop


This entry has been viewed 83 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express