Textbox numerical input

liampog

Active Member
Joined
Aug 3, 2010
Messages
312
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

I have the following code which accepts numbers 0-9 in a textbox.

Code:
Private Sub txtb_Amount_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    
    Select Case KeyAscii
    
        Case Asc("0")
        
            If txtb_Amount.Value = 0 Then KeyAscii = 0
    
        Case Asc("1") To Asc("9")
        
        Case Else
        
            KeyAscii = 0
    
    End Select


End Sub

However, the default value is 0 and if you try to type numbers to the RIGHT of the zero (other than 0 again) it will allow it "01111". I don't want it to do that, I want that if you start to type a number other than zero, it changes the value to the first number you type.

So if default value is "0" and then you type a "1" to the RIGHT of the zero, it changes the whole textbox value to 1 and then allows you to continue typing numbers.

Thanks in advance for your help.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi

Nevermind, I worked it out for myself :-)

This worked:

Code:
Private Sub txtb_Amount_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    
    Select Case KeyAscii


        Case Asc("0")
        
            If txtb_Amount.Value = 0 Then KeyAscii = 0


        Case Asc("1") To Asc("9")
        
            If (txtb_Amount.Value = 0 And txtb_Amount.SelStart = 1) Then KeyAscii = 0


        Case Else


            KeyAscii = 0


    End Select


End Sub
 
Upvote 0
Okay, so this works but what I would ideally like is if you DO happen to enter a number to the RIGHT of the existing default 0, I want it to change the value of the box to that number that you enter and then allow numbers to be entered afterwards.

The only trouble is it fires up the Change event. Is there any way of disabling the Change event?

Code:
Private Sub txtb_Amount_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    
    Select Case KeyAscii


        Case Asc("0")
        
            If txtb_Amount.Value = 0 Then KeyAscii = 0


        Case Asc("1") To Asc("9")
        
            If (txtb_Amount.Value = 0 And txtb_Amount.SelStart = 1) Then txtb_Amount.Value = Chr(KeyAscii)


        Case Else


            KeyAscii = 0


    End Select


End Sub
 
Upvote 0
Code:
Private Sub txtb_Amount_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  Static bItsjustMe     As Boolean

  If Not bItsjustMe Then
    bItsjustMe = True
    Select Case KeyAscii
      Case Asc("0")
        If txtb_Amount.Value = 0 Then KeyAscii = 0
      Case Asc("1") To Asc("9")
        If (txtb_Amount.Value = 0 And txtb_Amount.SelStart = 1) Then txtb_Amount.Value = Chr(KeyAscii)
      Case Else
        KeyAscii = 0
    End Select
    bItsjustMe = False
  End If
End Sub
 
Upvote 0
Hi

This still allowed numbers to be entered to the RIGHT of the existing zero in the box.

Thanks
Liam
 
Upvote 0
I was addressing this:

The only trouble is it fires up the Change event. Is there any way of disabling the Change event?

You said you'd sorted the other problem -- I didn't look at the rest of the code.
 
Upvote 0
Oh okay thanks for your reply though. It worked for the problem highlighted.
 
Upvote 0
If you start from the top and explain what you're trying to do, I'll look.

I don't understand at all from your first post. What's the objective?
 
Upvote 0
If you start from the top and explain what you're trying to do, I'll look.

I don't understand at all from your first post. What's the objective?

Basically, I want to be able to enter a number into a textbox. I have the relevant KeyPress event set up to only accept numbers. The default value is 0, however, if you place the cursor to the right of the default zero and type numbers, it allows them. For example "01111" would be an acceptable number. I wanted to code is to that if you do happen to have the cursor to the right of the default zero and enter a number "1" for example, it changes the 0 to a 1 instead of displaying "01".

I actually managed to work it all out though now :-)

Thanks again.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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