PDA

View Full Version : Solved: Formatting a number



ffcgrant
09-26-2011, 11:22 AM
I have a UserForm field where a person enters a number.

I want to make sure that the number has a comma after 3 digits ex: 5,000.

The hard part (at least for me) is that a person could enter cents or not.
ex: 5000.55 needs to turn into 5,000.55 and
5000 needs to turn into 5,000 and
125000.55 needs to turn into 125,000.55
Or, if the already entered a comma (5,000) it wouldn't need to do anything.

I've done a lot of searching and after many trials I still cannot get this. (new to VBA). It is probably an easy question...

Thanks for the help!

gmaxey
09-27-2011, 04:36 PM
Private Sub CommandButton1_Click()
Dim strNum As String
If IsNumeric(TextBox1.Text) Then
strNum = FormatCurrency(TextBox1.Text, , False, True, vbUseDefault)
Select Case True
Case Right(strNum, 3) = ".00"
strNum = Left(strNum, Len(strNum) - 3)
End Select
MsgBox strNum
End If
End Sub

ffcgrant
09-28-2011, 11:30 AM
Thank you, worked perfectly. :yes !