Is it possible to modify a text box in order that while you are filling it with numbers, they get separated by a comma? Not for a later print, just while you fill it.
Printable View
Is it possible to modify a text box in order that while you are filling it with numbers, they get separated by a comma? Not for a later print, just while you fill it.
You can do it from either the Change, Enter, or KeyPress events. Probably the change event will be best. Just use the Replace function and replace the comma with a vbnullstring.
What I meant was that I use Format function to format it with a comma for a later print. That's working fine. But I think it would be freindlier for the user if instead to entering 230000 and then seeing printed 230,000; the comma appeared instantly after filling the fourth character, between it and the third.
Is this a currency value? If it is then you can apply formatting for it in the Exit event then.
VB Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean) TextBox1.Text = Format(TextBox1.Text, "###,###,###") End Sub
What if the 4th character was the last character with the 2300 being entered instead of 23000 or 230000? How is the program supposed to know, before hand, that what the number will be?Quote:
Originally Posted by Fonty
I think the way you are doing is the best. Unless, of course, the numbers being entered will ALWAYS be six characters in length.
Hack, have you tried my code? It will format it from right to left. so if you enter in 1234 it weill be 1,234. :D
The format mask is the solution here. ;)
:lol: Yes, this unworthy one is aware that your code will format from right to left, and has used same on numerous occasions. :DQuote:
Originally Posted by RobDog888
However, unless I am mistaken, your code will format the number after the number has been completely entered, and focus has been sifted to another control. My comments were directed at Fonty and his desire to format the number, as it was typed, based on the numeric position of the character currently being typed.
I can see from the thread being resolved that the event didnt matter :D
But for others you can do like this to format as you type. :)
VB Code:
Private Sub TextBox1_Change() TextBox1.Text = Format(TextBox1.Text, "###,###,###") End Sub