Change Event - Textbox = > < Zero

03856me

Active Member
Joined
Apr 4, 2008
Messages
297
I have the following code setup in a change event which works perfectly unless the user enters any number preceeded by a zero, like 02 03 etc., then it fires the <= code which isn't correct. Is there some code I can add that would prevent this? I even tried it in the exit event but it worked the same.

There are 3 possible entries that are acceptable
0 should fire first code
-# should fire first code
1-99 should fire second code

Maybe there's a better way to go about this? I am open to any suggestions

Code:
Private Sub txt_dbh_Change()
'check the value is negative or equal to zero
  If Me.txt_dbh.Value <= 0 Then
     Me.txt_height.Visible = False
     Me.lbl_height.Visible = False
     Me.lbl_heightNote.Visible = False
     Me.txt_1log.Visible = False
     Me.lbl_1log.Visible = False
     Me.cmd_add.Visible = False
     Me.cmd_addNegative.Visible = True
     Cancel = True
'check the value is > 0
Else
  If Me.txt_dbh.Value > 0 Then
     Me.txt_height.Visible = True
     Me.lbl_height.Visible = True
     Me.lbl_heightNote.Visible = True
     Me.txt_1log.Visible = True
     Me.lbl_1log.Visible = True
     Cancel = True
   End If
  End If
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
a quick fix might be instead of:
If Me.txt_dbh.Value
have:
If val(Me.txt_dbh.Value)
 
Upvote 0
Hi,

Your code is not logic to my sense.

When you write "23" for instance, the code would fire twice.
When you want to input "-1", you would first get the ">0" code when writing the minus sign.
If only numbers are to be put there, you migh consider a listbox with those numbers.
Another possibility would be to ask for a number, then click a button which would check if it's really a number and then fire the code.

The line "cancel = true" makes no sense. It belongs to another kind of event like "sheet rightclick".

kind regards,
Erik
 
Upvote 0

Forum statistics

Threads
1,224,272
Messages
6,177,634
Members
452,787
Latest member
BeeTH

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