+ Reply to Thread
Results 1 to 2 of 2

Numerical value into text

  1. #1
    Registered User
    Join Date
    02-06-2015
    Location
    UK
    MS-Off Ver
    2014
    Posts
    1

    Numerical value into text

    Just trying to put a code into my spreadsheet to turn a numerical value into words such as for a payment or a cheque i.e. £123.45 would be one hundred and twenty three pounds and forty five pence only. Right now I have the below which only converts the number into text.
    • Is it possible to add the words pounds and pence to the end of values?
    • The numbers after decimal places are treated individually, how would I fix that? (e.g. 22 should be twenty two but it comes out as two two )


    thanks

    Option Explicit

    Public Numbers As Variant, Tens As Variant

    Sub SetNums()
    Numbers = Array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen")
    Tens = Array("", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety")
    End Sub

    Function WordNum(MyNumber As Double) As String
    Dim DecimalPosition As Integer, ValNo As Variant, StrNo As String
    Dim NumStr As String, n As Integer, Temp1 As String, Temp2 As String
    ' This macro was written by Chris Mead - www.MeadInKent.co.uk
    If Abs(MyNumber) > 999999999 Then
    WordNum = "Value too large"
    Exit Function
    End If
    SetNums
    ' String representation of amount (excl decimals)
    NumStr = Right("000000000" & Trim(Str(Int(Abs(MyNumber)))), 9)
    ValNo = Array(0, Val(Mid(NumStr, 1, 3)), Val(Mid(NumStr, 4, 3)), Val(Mid(NumStr, 7, 3)))
    For n = 3 To 1 Step -1 'analyse the absolute number as 3 sets of 3 digits
    StrNo = Format(ValNo(n), "000")
    If ValNo(n) > 0 Then
    Temp1 = GetTens(Val(Right(StrNo, 2)))
    If Left(StrNo, 1) <> "0" Then
    Temp2 = Numbers(Val(Left(StrNo, 1))) & " hundred"
    If Temp1 <> "" Then Temp2 = Temp2 & " and "
    Else
    Temp2 = ""
    End If
    If n = 3 Then
    If Temp2 = "" And ValNo(1) + ValNo(2) > 0 Then Temp2 = "and "
    WordNum = Trim(Temp2 & Temp1)
    End If
    If n = 2 Then WordNum = Trim(Temp2 & Temp1 & " thousand " & WordNum)
    If n = 1 Then WordNum = Trim(Temp2 & Temp1 & " million " & WordNum)
    End If
    Next n
    NumStr = Trim(Str(Abs(MyNumber)))
    ' Values after the decimal place
    DecimalPosition = InStr(NumStr, ".")
    Numbers(0) = "Zero"
    If DecimalPosition > 0 And DecimalPosition < Len(NumStr) Then
    Temp1 = " point"
    For n = DecimalPosition + 1 To Len(NumStr)
    Temp1 = Temp1 & " " & Numbers(Val(Mid(NumStr, n, 1)))
    Next n
    WordNum = WordNum & Temp1
    End If
    If Len(WordNum) = 0 Or Left(WordNum, 2) = " p" Then
    WordNum = "Zero" & WordNum
    End If
    End Function

    Function GetTens(TensNum As Integer) As String
    ' Converts a number from 0 to 99 into text.
    If TensNum <= 19 Then
    GetTens = Numbers(TensNum)
    Else
    Dim MyNo As String
    MyNo = Format(TensNum, "00")
    GetTens = Tens(Val(Left(MyNo, 1))) & " " & Numbers(Val(Right(MyNo, 1)))
    End If
    End Function

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Numerical value into text

    Hi

    Microsoft published a User Defined Function That does This.

    http://support.microsoft.com/kb/213360
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Text to Numerical Value
    By peteuga in forum Excel General
    Replies: 2
    Last Post: 01-26-2011, 04:22 PM
  2. Text to Numerical Value
    By peteuga in forum Excel General
    Replies: 3
    Last Post: 01-26-2011, 03:55 PM
  3. Text returning a numerical value
    By slinger1010 in forum Excel General
    Replies: 3
    Last Post: 11-14-2006, 08:24 AM
  4. Numerical Value to text
    By no0dles in forum Excel - New Users/Basics
    Replies: 7
    Last Post: 04-25-2006, 10:30 AM
  5. VLOOKUP first numerical value (not text)
    By titushanke in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-16-2005, 10:52 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1