To type negative value in textbox (Userform)

Anell

Board Regular
Joined
Sep 18, 2009
Messages
102
Good morning

I would appreciate some help with the following:

I've got a Userform that a user uses for auditing purposes.

The user would like to enter a negative value in some text boxes, but the moment one type a "-" in the text box, a "0" appears.

I believe I will have to change formatting or something but is not sure of the code?

Otherwise I can use combo boxes which will enable the user to select a negative value, but it will be quite a job to change the 37 text boxes to combo boxes and I assume there must be an easier way ...

Thanks! :)
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
It looks like you have something like this in the KeyPress Event of the textboxes:

If KeyAscii.Value = 45 Then KeyAscii.Value = 48

45 is the KeyAscii Value for "-" and 48 is for "0". Remove that line and it should solve it.
 
Upvote 0
Good morning

This is very interesting! Will the "45" enable the use of both a "-" and a "+" in one textbox?

I created the userform myself and there is no such indication in the code, but I will try your code to enable the textbox to accept a negative value.

Regards
Anéll

:rolleyes:
 
Upvote 0
Each (almost) key pressed on the keyboard has an ascii value attached to it. You can play with the result produced by pressing each key by assigning a different ascii value or do nothing by assigning a 0 to it.

However, if you have created the userform yourself and have not written any code specifically change the behaviour of a key, I am not sure why it would automatically behave differently.

Two possibilities I can think at this hour of the day (haven't slept for 2 nights):
1. There is problem with your keyboard
2. I have totally misunderstood your definition of "Userform" and "Textbox"

Oh by the way, the KeyAscii for "-" is 45, for "+" is 43, for "0" is 48
 
Last edited:
Upvote 0
Well, having no sleep for 2 nights may be a bit hard on anyone I guess! Although I will not be able to function at all if I was in your shoes!

The information you gave me is however very interesting - I am rather green still where it comes to VBA coding and it is the first time that a user has the specific need to be able to type a negative value into a textbox used in the userform.

I will play around with the info that you gave me and see whether it helps or not.

Hope you get some rest soon ..! :eek:

Regards
Anéll
 
Upvote 0
Hi!

I came upon the following code and it seems as if it is doing the job:

Selection.NumberFormat = "-0.00,0.00"

Thanks anyway for your help!

Regards
Anéll
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,037
Members
449,062
Latest member
mike575

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