limit decimal numbers displayed on textbox

adelvoira

New Member
Joined
Sep 9, 2017
Messages
35
hello, i have a problem with my textboxes to display decimal numbers with just two (2) decimals no more,
i tried this code:
Code:
Private Sub TextBox75_Change()
If IsNumeric(TextBox75.Value) Then
TextBox75.Text = Format(TextBox75, "0.00")
End If
End Sub

and it's not working good as you show in the attached screen caption
https://ibb.co/igUVP6

Thank you
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi, you could try like this:

Code:
Private Sub TextBox75_Change()
If IsNumeric(TextBox75.Value) Then
  TextBox75.Text = Format(cdbl(TextBox75.Value), "0.00")
End If
End Sub
 
Last edited:
Upvote 0
Rather than the Change event, you might try the AfterUpdate event so the textbook doesn't keep changing as the user types into it.

Add code like this to handle the textbox being filled programmatically

Code:
Private Sub TextBox75_AfterUpdate()
    If IsNumeric(TextBox75.Value) Then
        TextBox75.Text = Format(cdbl(TextBox75.Value), "0.00")
    End If
End Sub

Private Sub TextBox75_Change()
    With TextBox75.Parent
        If .ActiveControl.Name <> "TextBox75" Then
            Call TextBox75_AfterUpdate
        End If
    End With
End Sub
The .Parent is there to handle textboxes that are in Frames or Multipages
 
Last edited:
Upvote 0
The code didn't work when i use Microsoft office 2013 because i tried on Microsoft office 2010 on first time
 
Upvote 0
Which code didn't work? and what do you mean "didn't work"? What did happen that wasn't what you wanted?
 
Upvote 0
The first one

Do you mean the code I posted? If so I'd try Mike's if I were you, I didn't notice that the code I posted was inappropiately using the change event.

it's still display more than two decimals

It would help if you told us the unformatted value of the text box, what your locale decimal seperator is and what the formatted value should have looked like?
 
Upvote 0
yes i meant this code
Code:
[LEFT][COLOR=#333333][FONT=monospace]Private Sub TextBox75_Change()
If IsNumeric(TextBox75.Value) Then
  TextBox75.Text = Format(cdbl(TextBox75.Value), "0.00")
End If
End Sub[/FONT][/COLOR][/LEFT]
look to understand me well, i want from each texbox on my userform around all values to just two decimals (290298.59 or arround it to 290298.60) as you show on my screen caption below:
https://postimg.org/image/c5b62zjfv/
c5b62zjfv
 
Upvote 0
look to understand me well

Hi, I do understand you well. Have you tried Mike's code? What is the unformatted value that you are entering in the text box? What is your locale decimal seperator? And do you want the formatted value to look like after the code has executed.

Try to answer directly in a reply here.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
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