Turn numbers written as words into digits (two - 2, three - 3...)

analyst0503

New Member
Joined
Aug 5, 2021
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Hello,

I would like to find a formula to turn the right column into the left column.

Is there a way to turn the word twenty four into 24?

1630664971595.png



This would significantly speed up my work so I would appreciate your help!
Thanks in advance.
 
Hello everyone,

thank you for all your responses.
I'm sad to say I haven't used OPs or UDFs before so I'd need an explanation like the one for the OP to try and repeat that. Therefore, I'll have to google how the UDF option works. The OP option sounds like something I could try with, if anyone has that specific combination, that would be very helpful.

Looking forward to your suggestions! Thanks!
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
HOW TO INSTALL UDFs
------------------------------------
If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use NameOfTheUDF just like it was a built-in Excel function. For example,

=NameOfTheUDF(A1)

If you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Obviously, use the actual UDF name in place of my example "NameOfTheUDF".
 
Upvote 0
Following Rick's post, here's what I used:

WordsToNumber.xlsm
ABCDEF
1eight8one
2twenty-four24two
3seven7three
4nineteen19four
5twenty-nine29five
6seventy-five75six
7eighty-six86seven
8one hundred100eight
Sheet1
Cell Formulas
RangeFormula
B1:B8B1=MATCH(A1,$F$1:$F$100,0)
F1:F8F1=EnglishNumber(ROW())


with this UDF (can't remember the source, unfortunately). Also, the number isn't currency necessarily:

Code:
Function EnglishNumber(ByVal N As Currency) As String

   Const Thousand = 1000@
   Const Million = Thousand * Thousand
   Const Billion = Thousand * Million
   Const Trillion = Thousand * Billion

   If (N = 0@) Then EnglishNumber = "zero": Exit Function

   Dim Buf As String: If (N < 0@) Then Buf = "negative " Else Buf = ""
   Dim Frac As Currency: Frac = Abs(N - Fix(N))
   If (N < 0@ Or Frac <> 0@) Then N = Abs(Fix(N))
   Dim AtLeastOne As Integer: AtLeastOne = N >= 1

   If (N >= Trillion) Then
      Buf = Buf & EnglishNumberDigitGroup(Int(N / Trillion)) & " trillion"
      N = N - Int(N / Trillion) * Trillion
      If (N >= 1@) Then Buf = Buf & " "
   End If

   If (N >= Billion) Then
      Buf = Buf & EnglishNumberDigitGroup(Int(N / Billion)) & " billion"
      N = N - Int(N / Billion) * Billion
      If (N >= 1@) Then Buf = Buf & " "
   End If

   If (N >= Million) Then
      Buf = Buf & EnglishNumberDigitGroup(N \ Million) & " million"
      N = N Mod Million
      If (N >= 1@) Then Buf = Buf & " "
   End If

   If (N >= Thousand) Then
      Buf = Buf & EnglishNumberDigitGroup(N \ Thousand) & " thousand"
      N = N Mod Thousand
      If (N >= 1@) Then Buf = Buf & " "
   End If

   If (N >= 1@) Then
      Buf = Buf & EnglishNumberDigitGroup(N)
   End If

   EnglishNumber = Buf
End Function

Private Function EnglishNumberDigitGroup(ByVal N As Integer) As String

   Const Hundred = " hundred"
   Const One = "one"
   Const Two = "two"
   Const Three = "three"
   Const Four = "four"
   Const Five = "five"
   Const Six = "six"
   Const Seven = "seven"
   Const Eight = "eight"
   Const Nine = "nine"
   Dim Buf As String: Buf = ""
   Dim Flag As Integer: Flag = False

   Select Case (N \ 100)
      Case 0: Buf = "": Flag = False
      Case 1: Buf = One & Hundred: Flag = True
      Case 2: Buf = Two & Hundred: Flag = True
      Case 3: Buf = Three & Hundred: Flag = True
      Case 4: Buf = Four & Hundred: Flag = True
      Case 5: Buf = Five & Hundred: Flag = True
      Case 6: Buf = Six & Hundred: Flag = True
      Case 7: Buf = Seven & Hundred: Flag = True
      Case 8: Buf = Eight & Hundred: Flag = True
      Case 9: Buf = Nine & Hundred: Flag = True
   End Select

   If (Flag <> False) Then N = N Mod 100
   If (N > 0) Then
      If (Flag <> False) Then Buf = Buf & " "
   Else
      EnglishNumberDigitGroup = Buf
      Exit Function
   End If

   Select Case (N \ 10)
      Case 0, 1: Flag = False
      Case 2: Buf = Buf & "twenty": Flag = True
      Case 3: Buf = Buf & "thirty": Flag = True
      Case 4: Buf = Buf & "forty": Flag = True
      Case 5: Buf = Buf & "fifty": Flag = True
      Case 6: Buf = Buf & "sixty": Flag = True
      Case 7: Buf = Buf & "seventy": Flag = True
      Case 8: Buf = Buf & "eighty": Flag = True
      Case 9: Buf = Buf & "ninety": Flag = True
   End Select

   If (Flag <> False) Then N = N Mod 10
   If (N > 0) Then
      If (Flag <> False) Then Buf = Buf & "-"
   Else
      EnglishNumberDigitGroup = Buf
      Exit Function
   End If

   Select Case (N)
      Case 0:
      Case 1: Buf = Buf & One
      Case 2: Buf = Buf & Two
      Case 3: Buf = Buf & Three
      Case 4: Buf = Buf & Four
      Case 5: Buf = Buf & Five
      Case 6: Buf = Buf & Six
      Case 7: Buf = Buf & Seven
      Case 8: Buf = Buf & Eight
      Case 9: Buf = Buf & Nine
      Case 10: Buf = Buf & "ten"
      Case 11: Buf = Buf & "eleven"
      Case 12: Buf = Buf & "twelve"
      Case 13: Buf = Buf & "thirteen"
      Case 14: Buf = Buf & "fourteen"
      Case 15: Buf = Buf & "fifteen"
      Case 16: Buf = Buf & "sixteen"
      Case 17: Buf = Buf & "seventeen"
      Case 18: Buf = Buf & "eighteen"
      Case 19: Buf = Buf & "nineteen"
   End Select

   EnglishNumberDigitGroup = Buf

End Function
 
Upvote 0
Solution
I have tried using the above udf following your instructions, I now understand how this works which I'm very grateful for.
However, the provided function turns the number 2 into the word, whereas I need a way to turn the word two into the number 2.

Here is a screenshot:
1630864101965.png


As you can see, the formula returns a #VALUE result. It works only with turning the digits into words.
Thank you again and looking forward to any possible solutions.
 
Upvote 0
Following Rick's post, here's what I used:

WordsToNumber.xlsm
ABCDEF
1eight8one
2twenty-four24two
3seven7three
4nineteen19four
5twenty-nine29five
6seventy-five75six
7eighty-six86seven
8one hundred100eight
Sheet1
Cell Formulas
RangeFormula
B1:B8B1=MATCH(A1,$F$1:$F$100,0)
F1:F8F1=EnglishNumber(ROW())


with this UDF (can't remember the source, unfortunately). Also, the number isn't currency necessarily:

Code:
Function EnglishNumber(ByVal N As Currency) As String

   Const Thousand = 1000@
   Const Million = Thousand * Thousand
   Const Billion = Thousand * Million
   Const Trillion = Thousand * Billion

   If (N = 0@) Then EnglishNumber = "zero": Exit Function

   Dim Buf As String: If (N < 0@) Then Buf = "negative " Else Buf = ""
   Dim Frac As Currency: Frac = Abs(N - Fix(N))
   If (N < 0@ Or Frac <> 0@) Then N = Abs(Fix(N))
   Dim AtLeastOne As Integer: AtLeastOne = N >= 1

   If (N >= Trillion) Then
      Buf = Buf & EnglishNumberDigitGroup(Int(N / Trillion)) & " trillion"
      N = N - Int(N / Trillion) * Trillion
      If (N >= 1@) Then Buf = Buf & " "
   End If

   If (N >= Billion) Then
      Buf = Buf & EnglishNumberDigitGroup(Int(N / Billion)) & " billion"
      N = N - Int(N / Billion) * Billion
      If (N >= 1@) Then Buf = Buf & " "
   End If

   If (N >= Million) Then
      Buf = Buf & EnglishNumberDigitGroup(N \ Million) & " million"
      N = N Mod Million
      If (N >= 1@) Then Buf = Buf & " "
   End If

   If (N >= Thousand) Then
      Buf = Buf & EnglishNumberDigitGroup(N \ Thousand) & " thousand"
      N = N Mod Thousand
      If (N >= 1@) Then Buf = Buf & " "
   End If

   If (N >= 1@) Then
      Buf = Buf & EnglishNumberDigitGroup(N)
   End If

   EnglishNumber = Buf
End Function

Private Function EnglishNumberDigitGroup(ByVal N As Integer) As String

   Const Hundred = " hundred"
   Const One = "one"
   Const Two = "two"
   Const Three = "three"
   Const Four = "four"
   Const Five = "five"
   Const Six = "six"
   Const Seven = "seven"
   Const Eight = "eight"
   Const Nine = "nine"
   Dim Buf As String: Buf = ""
   Dim Flag As Integer: Flag = False

   Select Case (N \ 100)
      Case 0: Buf = "": Flag = False
      Case 1: Buf = One & Hundred: Flag = True
      Case 2: Buf = Two & Hundred: Flag = True
      Case 3: Buf = Three & Hundred: Flag = True
      Case 4: Buf = Four & Hundred: Flag = True
      Case 5: Buf = Five & Hundred: Flag = True
      Case 6: Buf = Six & Hundred: Flag = True
      Case 7: Buf = Seven & Hundred: Flag = True
      Case 8: Buf = Eight & Hundred: Flag = True
      Case 9: Buf = Nine & Hundred: Flag = True
   End Select

   If (Flag <> False) Then N = N Mod 100
   If (N > 0) Then
      If (Flag <> False) Then Buf = Buf & " "
   Else
      EnglishNumberDigitGroup = Buf
      Exit Function
   End If

   Select Case (N \ 10)
      Case 0, 1: Flag = False
      Case 2: Buf = Buf & "twenty": Flag = True
      Case 3: Buf = Buf & "thirty": Flag = True
      Case 4: Buf = Buf & "forty": Flag = True
      Case 5: Buf = Buf & "fifty": Flag = True
      Case 6: Buf = Buf & "sixty": Flag = True
      Case 7: Buf = Buf & "seventy": Flag = True
      Case 8: Buf = Buf & "eighty": Flag = True
      Case 9: Buf = Buf & "ninety": Flag = True
   End Select

   If (Flag <> False) Then N = N Mod 10
   If (N > 0) Then
      If (Flag <> False) Then Buf = Buf & "-"
   Else
      EnglishNumberDigitGroup = Buf
      Exit Function
   End If

   Select Case (N)
      Case 0:
      Case 1: Buf = Buf & One
      Case 2: Buf = Buf & Two
      Case 3: Buf = Buf & Three
      Case 4: Buf = Buf & Four
      Case 5: Buf = Buf & Five
      Case 6: Buf = Buf & Six
      Case 7: Buf = Buf & Seven
      Case 8: Buf = Buf & Eight
      Case 9: Buf = Buf & Nine
      Case 10: Buf = Buf & "ten"
      Case 11: Buf = Buf & "eleven"
      Case 12: Buf = Buf & "twelve"
      Case 13: Buf = Buf & "thirteen"
      Case 14: Buf = Buf & "fourteen"
      Case 15: Buf = Buf & "fifteen"
      Case 16: Buf = Buf & "sixteen"
      Case 17: Buf = Buf & "seventeen"
      Case 18: Buf = Buf & "eighteen"
      Case 19: Buf = Buf & "nineteen"
   End Select

   EnglishNumberDigitGroup = Buf

End Function
Sorry, I'm reading through this again and it looks like something that could work, could I ask you to explain a bit more the steps I need to take? Do I have to create a range a numbers from 1 - 100 (?) and convert them with the EnglishNumber function?

FORGOT WHAT I SAID! IT WORKED YAY!

If there are any other ways to do this in one step, that would be great but will be using this for now!
 
Upvote 0
Another take at the problem although there is already a solution. First draft and probably not too perfect but it works on NATURAL numbers (or whatever the term is in English). No source, just made it up myself
A different (direct) approach ;)
VBA Code:
Option Explicit

Public Function WordsToNumber(Words As String)
    Dim result, tmp
    Dim replW, replN
    Dim i As Long
    result = LCase$(Words)
    If Left(result, 2) = "a " Then result = "one " & Mid(result, 3)
  
'# Cleaning up potentially unwanted symbols - replace with SPACE
    replW = Array(",", "-", "_", """", " and ", "  ", "  ")
    For i = LBound(replW) To UBound(replW)
        result = Replace(result, replW(i), " ", , , vbTextCompare)
    Next i
    tmp = SplitUnderThousand(result)
    result = SplitOverThousands(tmp)
    tmp = Evaluate(result)
    If IsError(tmp) Then tmp = result
    WordsToNumber = tmp
    result = Null
    replW = Null
    replN = Null
    tmp = Null
End Function

Function SplitUnderThousand(ByVal Words As String)
    Dim result, tsum
    Dim replW, replN
    Dim i As Long, j As Long
    result = Words
    Const hun = "hundred"
  

'# I have reworked SpellNumber (from MS) to be used in another language, besides English _
    I guess a similar thing can be done here, although in Bulgarian it is not too straight-forward
    replW = Array("one", "two", "three", "four", "five", _
                "six", "seven", "eight", "nine", "ten", _
                "eleven", "twelve", "thirteen", "fourteen", "fifteen", _
                "sixteen", "seventeen", "eighteen", "nineteen", _
                "ninety", "eighty", "seventy", "sixty", "fifty", "forty", "thirty", "twenty")
    replN = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, _
                90, 80, 70, 60, 50, 40, 30, 20)
  
    result = Split(result, " ")
    For i = LBound(replW) To UBound(replW)
        For j = LBound(result) To UBound(result)
            If result(j) = replW(i) Then result(j) = "+" & replN(i)
        Next j
    Next i
    tsum = Join(result, " ")
    result = Replace(tsum, hun, "*100")
  
    SplitUnderThousand = result
  
    result = Null
    replN = Null
    replW = Null
End Function

Function SplitOverThousands(ByVal Words As String)
    Dim result, tmp, tmp2, tsum
    Dim replW, replN
    Dim i As Long, j As Long
    result = Words
  
    replW = Array("trillion", "billion", "million", "thousand")
    replN = Array(10 ^ 12, 10 ^ 9, 10 ^ 6, 1000)
  
    For i = LBound(replW) To UBound(replW)
        If InStr(1, result, replW(i), vbTextCompare) Then
            result = Split(result, replW(i))
            tmp = result(0)
            If IsNull(tsum) Then
                tsum = tmp & "*" & replN(i)
            Else
                tsum = tsum & "+(" & tmp & ")*" & replN(i)
            End If
            result = result(1)
        End If
    Next i
    result = tsum & result
    SplitOverThousands = result
End Function
 
Upvote 0

Forum statistics

Threads
1,223,703
Messages
6,173,941
Members
452,539
Latest member
delvey

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