+ Reply to Thread
Results 1 to 6 of 6

VBA Code for Percentage value to English Word

Hybrid View

  1. #1
    Registered User
    Join Date
    07-28-2011
    Location
    Philippines
    MS-Off Ver
    Excel 2010
    Posts
    80

    VBA Code for Percentage value to English Word

    Hi everyone,


    What is the VBA code for converting percentage value to English Word.

    Example:
    85.51 = Eighty Five point Fifty One Percent
    91.00 = Ninety One Percent
    7.11 = Seven point Eleven Percent


    Thank you in advance
    Last edited by chergian; 03-15-2017 at 04:57 AM.

  2. #2
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: VBA Code for Percentage value to English Word

    Maybe :
    'Function F_convert() and F_mats() are taken from "http://www.snb-vba.eu/VBA_Getallen_naar_tekst_en.html"
    Private Function F_convert(y)
      F_convert = "Invalid input"
      'If y = "" Or Val(y) = 0 Then Exit Function
      If y = "" Then Exit Function
      c00 = Format(Val(1 * y), String(3 * ((Len(Format(Val(1 * y))) - 1) \ 3 + 1), "0"))
      For j = 1 To Len(c00) \ 3
          x = Mid(c00, 3 * (j - 1) + 1, 3)
          sp = Array(F_mats(Left(x, 1)), F_mats(Val(Right(x, 2))), F_mats(Right(x, 1)), F_mats(Mid(x, 2, 1) & "0"), F_mats(Mid(x, 2, 1)))
          c01 = c01 & IIf(sp(0) = "", "", sp(0) & " Hundred ") & IIf(Right(x, 2) = "00", "", IIf(sp(1) <> "", sp(1), IIf(Mid(x, 2, 1) = "1", Trim(sp(2)) & "teen", IIf(sp(3) = "", sp(4) & "ty", sp(3)) & " " & sp(2)))) & Choose(Len(c00) \ 3 - j + 1, "", " Thousand ", " Million ", " Billion ")
      Next
      F_convert = IIf(c01 = "", "zero", Replace(c01, " ", " "))
    End Function
    Private Function F_mats(y)
      On Error Resume Next
      F_mats = Split(Split(" 0 1One 2Two 3Three 4Four 5Five 6Six 7Seven 8Eight 9Nine 10Ten 11Eleven 12Twelve 13Thirteen 15Fifteen 20Twenty 30Thirty 50Fifty 80Eighty ", y)(1))(0)
    End Function
    Private Function F_percent(y As String)
      If Len(y) Then
         v = Split(y, ".")
         F_percent = Application.Proper(F_convert(CLng(v(0))))
         If UBound(v) = 1 Then F_percent = F_percent & " point " & F_convert(CLng(v(1)))
         F_percent = F_percent & " Percent"
      End If
    End Function
    
    
    Sub Test()
      Debug.Print F_percent("")
      Debug.Print F_percent("0")
      Debug.Print F_percent("85")
      Debug.Print F_percent("85.51")
      Debug.Print F_percent("7.11")
    End Sub
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  3. #3
    Registered User
    Join Date
    07-28-2011
    Location
    Philippines
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: VBA Code for Percentage value to English Word

    Quote Originally Posted by karedog View Post
    Maybe :
    'Function F_convert() and F_mats() are taken from "http://www.snb-vba.eu/VBA_Getallen_naar_tekst_en.html"
    Private Function F_convert(y)
      F_convert = "Invalid input"
      'If y = "" Or Val(y) = 0 Then Exit Function
      If y = "" Then Exit Function
      c00 = Format(Val(1 * y), String(3 * ((Len(Format(Val(1 * y))) - 1) \ 3 + 1), "0"))
      For j = 1 To Len(c00) \ 3
          x = Mid(c00, 3 * (j - 1) + 1, 3)
          sp = Array(F_mats(Left(x, 1)), F_mats(Val(Right(x, 2))), F_mats(Right(x, 1)), F_mats(Mid(x, 2, 1) & "0"), F_mats(Mid(x, 2, 1)))
          c01 = c01 & IIf(sp(0) = "", "", sp(0) & " Hundred ") & IIf(Right(x, 2) = "00", "", IIf(sp(1) <> "", sp(1), IIf(Mid(x, 2, 1) = "1", Trim(sp(2)) & "teen", IIf(sp(3) = "", sp(4) & "ty", sp(3)) & " " & sp(2)))) & Choose(Len(c00) \ 3 - j + 1, "", " Thousand ", " Million ", " Billion ")
      Next
      F_convert = IIf(c01 = "", "zero", Replace(c01, " ", " "))
    End Function
    Private Function F_mats(y)
      On Error Resume Next
      F_mats = Split(Split(" 0 1One 2Two 3Three 4Four 5Five 6Six 7Seven 8Eight 9Nine 10Ten 11Eleven 12Twelve 13Thirteen 15Fifteen 20Twenty 30Thirty 50Fifty 80Eighty ", y)(1))(0)
    End Function
    Private Function F_percent(y As String)
      If Len(y) Then
         v = Split(y, ".")
         F_percent = Application.Proper(F_convert(CLng(v(0))))
         If UBound(v) = 1 Then F_percent = F_percent & " point " & F_convert(CLng(v(1)))
         F_percent = F_percent & " Percent"
      End If
    End Function
    
    
    Sub Test()
      Debug.Print F_percent("")
      Debug.Print F_percent("0")
      Debug.Print F_percent("85")
      Debug.Print F_percent("85.51")
      Debug.Print F_percent("7.11")
    End Sub

    Thank you so much karedog.... But there are something wrong with .01 to .09, it's spell this way:

    Example:
    90.01 spell as "Ninety point One Percent" - instead "Ninety point Zero One Percent"
    90.02 spell as "Ninety point Two Percent" - instead "Ninety point Zero Two Percent"
    ...and up to .09...

    Hope you get my point...

  4. #4
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: VBA Code for Percentage value to English Word

    Replace the F_percent() function above with this :

    Private Function F_percent(y As String)
      If Len(y) Then
         v = Split(y, ".")
         F_percent = Application.Proper(F_convert(CLng(v(0))))
         If UBound(v) = 1 Then
            v(1) = Left$(v(1), 2)
            If Left$(v(1), 1) = "0" Then F_percent = Application.Proper(F_percent & " point Zero " & F_convert(CLng(Right$(v(1), 1)))) Else F_percent = Application.Proper(F_percent & " point " & F_convert(CLng(v(1))))
         End If
         F_percent = F_percent & " Percent"
      End If
    End Function
    And don't forget to mark this thread as solved :
    select Thread Tools from the menu link above and mark this thread as SOLVED.

  5. #5
    Registered User
    Join Date
    07-28-2011
    Location
    Philippines
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: VBA Code for Percentage value to English Word

    Quote Originally Posted by karedog View Post
    replace the f_percent() function above with this :

    private function f_percent(y as string)
      if len(y) then
         v = split(y, ".")
         f_percent = application.proper(f_convert(clng(v(0))))
         if ubound(v) = 1 then
            v(1) = left$(v(1), 2)
            if left$(v(1), 1) = "0" then f_percent = application.proper(f_percent & " point zero " & f_convert(clng(right$(v(1), 1)))) else f_percent = application.proper(f_percent & " point " & f_convert(clng(v(1))))
         end if
         f_percent = f_percent & " percent"
      end if
    end function
    and don't forget to mark this thread as solved :
    Select thread tools from the menu link above and mark this thread as solved.


    thank so much karedog... It works!

  6. #6
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: VBA Code for Percentage value to English Word

    You are welcome, thanks for marking the thread as solved and for the rep.points.

    Regards

+ 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. valid english word
    By zerodegree in forum Excel General
    Replies: 6
    Last Post: 12-30-2016, 12:52 PM
  2. how to convert numeric value to english word?
    By vengatvj in forum Excel General
    Replies: 6
    Last Post: 03-14-2015, 03:53 PM
  3. English Word to Numeric digit conversion of numbers- How to do it?
    By Dipyaman in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-18-2013, 06:29 PM
  4. convert number to english word
    By mimzkie1101 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-10-2012, 05:41 AM
  5. predefine english word on the cell
    By garyww in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-27-2006, 07:45 AM
  6. ms excel predefine english word on the cell
    By garyww in forum Excel General
    Replies: 1
    Last Post: 06-27-2006, 07:45 AM
  7. [SOLVED] How to convert english word to pronoun in excel?
    By nath in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-13-2006, 12:55 PM

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