Tweaking a UDF to decompose a number

andreascostas

Board Regular
Joined
Jan 11, 2011
Messages
150
How do I tweak this UDF further to get it to give me the output I need

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, " x 1)", " x 10)", " x 100)", " x 1,000)", " x 10,000)", " x 100,000)") & " + " & Decompose
Next
Decompose = Left(Decompose, Len(Decompose) - 3)
End If
End Function

The UDF as is gives me the following:
In Cell A1: 96,183 output in B1: 9 x 10,000) + 6 x 1,000) + 1 x 100) + 8 x 10) + 3 x 1)
I am missing the opening parenthesis “(“ for each set of numbers. I want it to show this in B1:
(9 x 10,000) + (6 x 1,000) + (1 x 100) + (8 x 10) +(3 x 1)…….A member gave me this UDF to accomplish
A different output. I played around with it to create an additional worksheet for my students,
But I am stuck. I can’t figure out where to put the “(“ to make it work.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
How do I tweak this UDF further to get it to give me the output I need

Function Decompose(ByVal Number As String) As String
Dim X As Long
Number = Replace(Number, ",", "")
If Number = 0 Then
Decompose = "(0 x 1)"
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, " x 1)", " x 10)", " x 100)", " x 1,000)", " x 10,000)", " x 100,000)") & " + " & Decompose
Next
Decompose = Left(Decompose, Len(Decompose) - 3)
End If
End Function
Add what I show in red above.
 
Last edited:
Upvote 0
Maybe adding the code line in red...

Code:
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, " x 1)", " x 10)", " x 100)", " x 1,000)", _
                " x 10,000)", " x 100,000)") & " + " & Decompose
        Next
        Decompose = Left(Decompose, Len(Decompose) - 3)
        [COLOR=#ff0000]Decompose = "(" & Replace(Decompose, "+ ", "+ (")[/COLOR]
    End If
End Function

M.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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