allow only numbers in a textbox1

tony.reynolds

Board Regular
Joined
Jul 8, 2010
Messages
97
Id like to only allow numbers in a textbox. but if a user enters any other character only remove that character

At the moment i have this and it obviously removes all characters if any other character is entered. bit of a pain

Should be easy but i cant seem to find a way to remove the last character entered

Code:
Private Sub BudgetAmount_Change()
Cancel.Caption = "Cancel"
Done.Enabled = True
If Not IsNumeric(BudgetAmount) Then
BudgetAmount = Empty
End If
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Maybe this you want.

Code:
Private Sub BudgetAmount_Change()
Cancel.Caption = "Cancel"
Done.Enabled = True
If (Not IsNumeric(BudgetAmount)) Then
BudgetAmount.Value = ""
End If
End Sub
 
Upvote 0
Thanks,

My code worked fine with exeption that if i typed 1234e for instance the whole 1234 is removed. i want it to remove or not allow the letter.
 
Upvote 0
Try:-
Code:
[FONT=Fixedsys]Private Sub BudgetAmount_Change()[/FONT]
[FONT=Fixedsys]Cancel.Caption = "Cancel"[/FONT]
[FONT=Fixedsys]Done.Enabled = True[/FONT]
[FONT=Fixedsys]If Not IsNumeric(BudgetAmount) Then[/FONT]
[FONT=Fixedsys][COLOR=red]  BudgetAmount = Left(BudgetAmount.Text, Len (BudgetAmount.Text) - 1)[/COLOR][/FONT]
[FONT=Fixedsys]End If[/FONT]
[FONT=Fixedsys]End Sub[/FONT]
 
Upvote 0
Hi

2 remarks:

IsNumeric() will return true if it's possible to convert the text value to number. In this case this means that it will accept for ex. spaces after the digits. I think, therefore, that is better if we test just the last character.

Also if you delete all the digits the IsNumeric() will error out and so it's better to test is the value is the null string.

For ex.:

Code:
If Len(BudgetAmount) > 0 Then
    If Not IsNumeric(Right(BudgetAmount.Text, 1)) Then
        BudgetAmount = Left(BudgetAmount.Text, Len(BudgetAmount.Text) - 1)
    End If
End If
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,928
Members
452,366
Latest member
TePunaBloke

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