Number format in Textbox

Good morning,

In my textbox4 i succed to give a format to the number like 1 254 254  but when i apply it to my textbox3 It doesn't work. I want it with decimals in my textbox 3

Thank you
Draft.xlsm
Adil KhAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Why did you place the code for TextBox3_KeyUp? It replaces the . with a , in textbox3.

I assume you were talking about the UserForm1.

Refer to the attached and see if that works as desired now.
Draft.xlsm
Roy CoxGroup Finance ManagerCommented:
You need to use the code  for each textbox as given in your previous post. e.g
Private Sub TextBox3_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
     
     ' '///Numeric values only
    Select Case KeyAscii
    Case 46 ' period
    Case 48 To 57 ' 0 to 9
    Case 43 ' +
    Case 45 ' -
    Case 49 ' backspace
    Case Else
        KeyAscii = vbNull
    End Select
     
End Sub

Open in new window


And as JKP said do not use the KeyDown event from the code and only use the KeyPress event.
Fabrice LambertConsultingCommented:
Side note:
- Give meaningfull names to your forms, controls and variables, so we (and later, you) can understand better what you are doing.
For instance, UserForm1, UserForm2 ect .. ComboBox1, TextBox1 ect ... are not meaningfull.

- Unless you have a very valid reason, do not use global variables. It is usually a sign of bad design.
As it stand, your workbook don't need any.

- Avoid using objects such as ActiveWorkbook, ActiveSheet, ActiveCell, ActiveChart, Selection (the worst one), the global Workbooks, Sheets, Cells, Range collections, because these are user dependant and chaotic objects, as a developper, you don't want to use chaotic objects.

- Within your forms, you don't need to constantly use the "Me" keyword. Forms are class, and each instance know itself.

- Discipline never hurt, use Option Explicit at the top of all your modules.

- Write error handlers.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Adil KhAuthor Commented:
@Subodh Tiwari (Neeraj) Yes Userform1 In texbox4 number as 1 000 000 no decimals are allowded. In textbox3 decimals are allowded but in this format 1 000 000,00 not like 1000000 Do you get what I mean?

@Roy Cox Thank you but when i applied it in textbox3 it accept no limit points (.) It should be only one point (.)

Thank you
Draft.xlsm
Adil KhAuthor Commented:
Thank you @Fabrice Lambert It is helpful as i am just a beginner I should learn.
Is there a document or website you recomand me to visit in order to develop those fondamentals?

Thank you
Fabrice LambertConsultingCommented:
I'll provide you something in private.

Concerning your issue, to avoid unlimited periods, check the control's value.
And for factorisation purpose, make a generic procedure that you'll call in all controls that need it:
Private Sub TextBox4_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
         ' '///Numeric values only
    restrictNumeric KeyAscii, TextBox4
End Sub

Open in new window

    '// Write this in a standard module
Public Sub restrictNumeric(ByRef KeyAscii As MSForms.ReturnInteger, ByRef ctrl As MSForms.Control)
    Select Case KeyAscii
    Case 46 ' period
        If (InStr(ctrl.Value, ".") > 0) Then
            KeyAscii = vbNull
        End If
    Case vbKey0 To vbKey9 ' 0 to 9
    Case 43 ' +
    Case 45 ' -
    Case 49 ' backspace
    Case Else
        KeyAscii = vbNull
    End Select
End Sub

Open in new window

Roy CoxGroup Finance ManagerCommented:
This code is messing the numbers up

total = Val(Replace(TextBox3, ",", ".")) * Val(Replace(TextBox4, ",", "."))

Open in new window


and this

'Private Sub TextBox3_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
'TextBox3.Value = Replace(TextBox3, ".", ",")
'End Sub

Open in new window


Try the attached.

Note, I have cancelled the DatePicker code because it doesn't work on my Excel. Using the DatePicker Control is inadvisable because it is often not available to users. I use a calendar form, look at the Calendar Form here, it's free to download.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Adil KhAuthor Commented:
Hi Fabrice,

I have an error when i apply your code

Please see the error attached
Pic.png
Adil KhAuthor Commented:
Hi Roy Rox,

Because I have a french Excel that's why we use . instead of , and vice versa

Thank you for the website helpful and interesting.
Adil KhAuthor Commented:
Hi Subodh Tiwari (Neeraj)

Thank you for the file but there is no format like 1 000 000.00 in txbox 3 only like 1000000.00

Thank you
Fabrice LambertConsultingCommented:
Hi Fabrice,

 I have an error when i apply your code
You did not follow my instructions !!!!!
Write this in a standard module
Roy CoxGroup Finance ManagerCommented:
Surely you still do not need the replace function.

TextBox3 works perfectly for me with my number format. I've amended the code to allow the use of ,
Draft--1-.xlsm
Adil KhAuthor Commented:
Thank you
Roy CoxGroup Finance ManagerCommented:
Pleased to help
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You're welcome Adil!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.