Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default ascii validation question


I have created the following code to only allow numeric values (an
decimal point) into TextBox1.

Private Sub Textbox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Asc("0") To Asc("99")
Case Asc(".")
If InStr(1, TextBox1.Text, ".") 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
End Sub
The problem is this allow the entry of the decimal point alone. Thi
causes a '#VALUE' error in the target cell which in turn causes th
code to halt and bring up the debug form.
How do I modify this code so that if the user attempts to enter th
decimal point without a trailing number the value gets turned to Null

--
SimonBianch
-----------------------------------------------------------------------
SimonBianchi's Profile: http://www.excelforum.com/member.php...fo&userid=3729
View this thread: http://www.excelforum.com/showthread.php?threadid=57011

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default ascii validation question

How about adding another check?

If TextBox1.Text = "" Then
KeyAscii = 0
End If

In the case asc(".") portion.

And watch that Asc("99"). I bet you meant asc("9").

====

But I don't think I'd stop this. But right before I was gonna use that value in
the textbox, I'd prefix it with a 0.

So if the user wanted to hit .5, they could.
I'd just stick a 0 in front (0.5) before processing.

Maybe even format the number on the way out:

Option Explicit
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With Me.TextBox1
.Text = Format("0" & .Text, "0.000")
End With
End Sub





SimonBianchi wrote:

I have created the following code to only allow numeric values (and
decimal point) into TextBox1.

Private Sub Textbox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Asc("0") To Asc("99")
Case Asc(".")
If InStr(1, TextBox1.Text, ".") 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
End Sub
The problem is this allow the entry of the decimal point alone. This
causes a '#VALUE' error in the target cell which in turn causes the
code to halt and bring up the debug form.
How do I modify this code so that if the user attempts to enter the
decimal point without a trailing number the value gets turned to Null?

--
SimonBianchi
------------------------------------------------------------------------
SimonBianchi's Profile: http://www.excelforum.com/member.php...o&userid=37298
View this thread: http://www.excelforum.com/showthread...hreadid=570116


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Validation Question RJ Swain Excel Worksheet Functions 8 February 10th 08 10:49 PM
Validation Question SiH23 Excel Discussion (Misc queries) 2 September 16th 07 08:29 PM
Question on Validation Manju Excel Worksheet Functions 3 January 12th 07 09:04 PM
validation question G Excel Discussion (Misc queries) 12 January 24th 06 10:55 PM
Validation Question....Can this be done? harpscardiff Excel Discussion (Misc queries) 2 January 11th 06 05:15 PM


All times are GMT +1. The time now is 03:17 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"