EXCEL FORMULA

  1.  

  1. Function Pcskill(ByVal MyNumber As Double) As String
  2.     Dim NumStr As String
  3.     Dim DecimalPlace As Integer, Count As Integer
  4.     Dim Temp As String, Tens As String


  5.     ReDim Place(9) As String
  6.     Place(2) = " Thousand "
  7.     Place(3) = " Million "
  8.     Place(4) = " Billion "
  9.     Place(5) = " Trillion "
  10.     
  11.     ' Convert MyNumber to string, trimming extra spaces.
  12.     NumStr = Trim(Str(MyNumber))


  13.     ' Find decimal place.
  14.     DecimalPlace = InStr(NumStr, ".")


  15.     ' Convert cents and set MyNumber to integer amount.
  16.     If DecimalPlace > 0 Then
  17.         Temp = GetTens(Left(Mid(NumStr, DecimalPlace + 1) & "00", 2))
  18. Pcskill = Temp & " Cents"
  19.         ' Strip off cents from remainder to convert.
  20.         NumStr = Trim(Left(NumStr, DecimalPlace - 1))
  21.     End If


  22.     Count = 1
  23.     Do While NumStr <> ""
  24.         Temp = GetHundreds(Right(NumStr, 3))
  25.         If Temp <> "" Then Pcskill = Temp & Place(Count) & Pcskill
  26.         If Len(NumStr) > 3 Then
  27.             NumStr = Left(NumStr, Len(NumStr) - 3)
  28.         Else
  29.             NumStr = ""
  30.         End If
  31.         Count = Count + 1
  32.     Loop
  33.     Pcskill = Trim(Pcskill)
  34. End Function


  35. ' Converts a number from 100-999 into text
  36. Function GetHundreds(ByVal MyNumber As String) As String
  37. Dim Result As String
  38.     If Val(MyNumber) = 0 Then Exit Function
  39.     MyNumber = Right("000" & MyNumber, 3) ' Ensure three digits
  40.     ' Convert the hundreds place.
  41.     If Mid(MyNumber, 1, 1) <> "0" Then
  42.         Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred "
  43.     End If
  44.     ' Convert the tens and ones place.
  45.     If Mid(MyNumber, 2, 1) <> "0" Then
  46.         Result = Result & GetTens(Mid(MyNumber, 2))
  47.     Else
  48.         Result = Result & GetDigit(Mid(MyNumber, 3))
  49.     End If
  50.     GetHundreds = Result
  51. End Function


  52. ' Converts a number from 10 to 99 into text.
  53. Function GetTens(TensText As String) As String
  54. Dim Result As String
  55.     Result = ""           ' Null out the temporary function value.
  56.     If Val(Left(TensText, 1)) = 1 Then   ' If value between 10-19...
  57.         Select Case Val(TensText)
  58.             Case 10: Result = "Ten"
  59.             Case 11: Result = "Eleven"
  60.             Case 12: Result = "Twelve"
  61.             Case 13: Result = "Thirteen"
  62.             Case 14: Result = "Fourteen"
  63.             Case 15: Result = "Fifteen"
  64.             Case 16: Result = "Sixteen"
  65.             Case 17: Result = "Seventeen"
  66.             Case 18: Result = "Eighteen"
  67.             Case 19: Result = "Nineteen"
  68.             Case Else
  69.         End Select
  70.     Else                                 ' If value between 20-99...
  71.         Select Case Val(Left(TensText, 1))
  72.             Case 2: Result = "Twenty "
  73.             Case 3: Result = "Thirty "
  74. Case 4: Result = "Forty "
  75.             Case 5: Result = "Fifty "
  76.             Case 6: Result = "Sixty "
  77.             Case 7: Result = "Seventy "
  78.             Case 8: Result = "Eighty "
  79.             Case 9: Result = "Ninety "
  80.             Case Else
  81.         End Select
  82.         Result = Result & GetDigit(Right(TensText, 1))   ' Retrieve ones place.
  83.     End If
  84.     GetTens = Result
  85. End Function


  86. ' Converts a number from 1 to 9 into text.
  87. Function GetDigit(Digit As String) As String
  88.     Select Case Val(Digit)
  89.         Case 1: GetDigit = "One"
  90.         Case 2: GetDigit = "Two"
  91.         Case 3: GetDigit = "Three"
  92.         Case 4: GetDigit = "Four"
  93.         Case 5: GetDigit = "Five"
  94.         Case 6: GetDigit = "Six"
  95. Case 7: GetDigit = "Seven"
  96.         Case 8: GetDigit = "Eight"
  97.         Case 9: GetDigit = "Nine"
  98.         Case Else: GetDigit = ""
  99.     End Select
  100. End Function

Comments

Popular posts from this blog