Dynamic Decimal Placement

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
1,074
Office Version
  1. 2019
Platform
  1. Windows
I using this code that formats the number as I type. I would like to make more dynamic to be able to choose the format base on a variable enter in another textbox. Currently the format is hard coded to two zeros after the decimal. I would like to enter (ex. 3) and the format would show $0.000

Any suggestions?

Thanks


Code:
Dim v As String


Select Case Len(tbOPrice)
    Case 1
        tbOPrice = Format(tbOPrice, "$0\.00")
    Case Is > 1
        v = Replace(tbOPrice, "$", "")
        v = Replace(v, ".", "")
        tbOPrice = Format(CCur(v) / 100, "$#,#0.00")
    Case Else
End Select
 
Last edited:

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Am no VBA expert, does this work?
You want something along the lines of

Code:
tbOPrice = Format(CCur(v) / 100, "$#,#0." & Right(10 ^ txtbox1,Len(10 ^ txtbox1)-1))

10^txtbox1 : If txtbox1 = 2, result is 100, if txtbox1 is 3 result is 1000
Performing a Right of that calculation using 1 less than the Length of it would knock the first digit off leaving 00 or 000 (I hope)
You can then concatenate that to your decimal "$#,#0."
 
Last edited:
Upvote 0
How do I adjust the codes in red. I have to manually change the numbers in red from 2 zeros to 3 zeros so that it does not cause an error.

Code:
[COLOR=#574123]CCur(v) / [/COLOR][COLOR=#ff0000][B]100[/B][/COLOR]
 
Last edited:
Upvote 0
Untested, but maybe this:

Code:
z = WorksheetFunction.Rept(0, textbox1.Value)
tbOPrice = Format(CCur(V) / 1 & z, "$#,#0." & z)
 
Upvote 0
Runtime error 6...Overflow

Output shows $1,000,000,000,000,000.000
 
Upvote 0
Hm, let's try this first:

Code:
Sub tryX()

z = WorksheetFunction.Rept(0, 3)
Debug.Print z
Debug.Print 1 & z
Debug.Print "$#,#0." & z

End Sub
The result in immediate window is:
000
1000
$#,#0.000

Isn't that what you expect?
 
Upvote 0
when I type in a value (ex 1),

If the textbox variable is 2, the output should be $1.00
If the textbox variable is 3, the output should be $1.000
 
Upvote 0
If I understand you correctly, maybe this:
Code:
Sub tryA()
x = 1 ' in tbOPrice
y = 3  'in the variable textbox

        V = Replace(x, "$", "")
        V = Replace(V, ".", "")
        z = WorksheetFunction.Rept(0, y)
        x = Format(x, "$#,#0." & z)

Debug.Print x
End Sub
 
Upvote 0
Correct...this is the output I'm looking for. Now the challenge is to implement it into the original code
 
Upvote 0

Forum statistics

Threads
1,213,507
Messages
6,114,029
Members
448,543
Latest member
MartinLarkin

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