Decomposing a number in expanded form

andreascostas

Board Regular
Joined
Jan 11, 2011
Messages
150
I want to Randomly generate a number from thousand to hundred thousand, and then decomposed it.

Example, input in cell A1: 234,789
Output in cell B1: 2 hundred thousands + 3 ten thousands + 4 thousands + 7 hundreds + 8 tens + 9 ones

Another example input in cell A1: 207,089 and output in B1: 2 hundred thousands + 7 thousands + 8 tens + 9 ones

Or A1: 23,005 output in B1: 2 ten thousands + 3 thousands + ones

In other words, I need it to accommodate zero digits without listing them. I can use left and right functions to do the ones that have no 0 in any of the digits, but am having trouble with examples like the Second and third example. Can a udf be written to accomplish this that can do multi[le ones at the same time?
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Give this UDF (user defined function) a try...
Code:
[table="width: 500"]
[tr]
	[td]Function Decompose(ByVal Number As String) As String
  Dim X As Long
  Number = Replace(Number, ",", "")
  If Number = 0 Then
    Decompose = "0 ones"
  Else
    For X = Len(Number) To 1 Step -1
      If Mid(Number, X, 1) Then Decompose = Mid(Number, X, 1) & Choose(Len(Number) - X + 1, " ones", " tens", " hundreds", " thousands", " ten thousand", " hundred thousand") & " + " & Decompose
    Next
    Decompose = Left(Decompose, Len(Decompose) - 3)
  End If
End Function
[/td]
[/tr]
[/table]

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 Decompose just like it was a built-in Excel function. For example,

=Decompose(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.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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