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:
Try this:
Code:
Sub tryB()
x = tbOPrice
        V = Replace(x, "$", "")
        x = Replace(V, ".", "")
        z = WorksheetFunction.Rept(0, textbox1)
        tbOPrice = Format(x, "$#,#0." & z)

End Sub
I have to ask:
You have this line: Replace(V, ".", "")
It means that you want to remove "." from the tbOPrice (before you convert the format), so if you type 1.2 then it will be 12, is it correct?
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
After deciphering what the code was doing, I figure it out how to merge your code with the original code. I reserched what Rept function does and this is what I put together. The red highlighted part needed it to be in parenthesis.

Thank you immensely for your help. Greatly appreciated it!

Code:
Private Sub tbOPrice_Change()Dim v As String


Z = WorksheetFunction.Rept(0, tbEFSet)


Select Case Len(tbOPrice)
    Case 1
        tbOPrice = Format(tbOPrice, "$0\." & Z)
    Case Is > 1
        v = Replace(tbOPrice, "$", "")
        v = Replace(v, ".", "")
        tbOPrice = Format([COLOR=#ff0000][B]v / (1 & Z)[/B][/COLOR], "$#,#0." & Z)
    Case Else
End Select
End Sub
 
Upvote 0
You're welcome, glad to help, & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,213,510
Messages
6,114,048
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