Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 65
Default Data Validation for % input in userfom

hi - I have a user entry text box to enter a % value...and a submit command
button which mutiplies the entry with another number. i'm able to take inputs
both as % and as whole numbers and based on the input..either remove the % or
if whole number then divide by 100. i would like to put a change event which
catches any input other than numers or %....valid entries would be 100% or
100.....need help with being able to catch entries like rree or rr%

any help much appreciated.

regards,
Amit
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Data Validation for % input in userfom

We can explicitly test the input:

Sub mesage()
x = Application.InputBox(Prompt:="Enter value:", Type:=2)
ok = False
If IsNumeric(x) Then
ok = True
Else
l = Len(x)
If l 1 Then
v1 = Right(x, 1)
v2 = Left(x, l - 1)
If v1 = "%" And IsNumeric(v2) Then
ok = True
End If
End If
End If

If ok Then
MsgBox ("data good")
Else
MsgBox ("data not valid")
End If
End Sub

We test if the data is a number or a number followed by the % character.

--
Gary''s Student - gsnu200785


"amit" wrote:

hi - I have a user entry text box to enter a % value...and a submit command
button which mutiplies the entry with another number. i'm able to take inputs
both as % and as whole numbers and based on the input..either remove the % or
if whole number then divide by 100. i would like to put a change event which
catches any input other than numers or %....valid entries would be 100% or
100.....need help with being able to catch entries like rree or rr%

any help much appreciated.

regards,
Amit

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Data Validation for % input in userfom

For your assignment to v2, I think you meant

v2 = Left(x, Len(x) - 1)

Rick


"Gary''s Student" wrote in message
...
We can explicitly test the input:

Sub mesage()
x = Application.InputBox(Prompt:="Enter value:", Type:=2)
ok = False
If IsNumeric(x) Then
ok = True
Else
l = Len(x)
If l 1 Then
v1 = Right(x, 1)
v2 = Left(x, l - 1)
If v1 = "%" And IsNumeric(v2) Then
ok = True
End If
End If
End If

If ok Then
MsgBox ("data good")
Else
MsgBox ("data not valid")
End If
End Sub

We test if the data is a number or a number followed by the % character.

--
Gary''s Student - gsnu200785


"amit" wrote:

hi - I have a user entry text box to enter a % value...and a submit
command
button which mutiplies the entry with another number. i'm able to take
inputs
both as % and as whole numbers and based on the input..either remove the
% or
if whole number then divide by 100. i would like to put a change event
which
catches any input other than numers or %....valid entries would be 100%
or
100.....need help with being able to catch entries like rree or rr%

any help much appreciated.

regards,
Amit


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Data Validation for % input in userfom

I just noticed... you defined the lower case "L" as being equal to Len(x).
That is a bad choice for a variable name... on my system, it looked like
your wrote One minus One rather than El minus One, hence my comment.

Rick


"Rick Rothstein (MVP - VB)" wrote in
message ...
For your assignment to v2, I think you meant

v2 = Left(x, Len(x) - 1)

Rick


"Gary''s Student" wrote in
message ...
We can explicitly test the input:

Sub mesage()
x = Application.InputBox(Prompt:="Enter value:", Type:=2)
ok = False
If IsNumeric(x) Then
ok = True
Else
l = Len(x)
If l 1 Then
v1 = Right(x, 1)
v2 = Left(x, l - 1)
If v1 = "%" And IsNumeric(v2) Then
ok = True
End If
End If
End If

If ok Then
MsgBox ("data good")
Else
MsgBox ("data not valid")
End If
End Sub

We test if the data is a number or a number followed by the % character.

--
Gary''s Student - gsnu200785


"amit" wrote:

hi - I have a user entry text box to enter a % value...and a submit
command
button which mutiplies the entry with another number. i'm able to take
inputs
both as % and as whole numbers and based on the input..either remove the
% or
if whole number then divide by 100. i would like to put a change event
which
catches any input other than numers or %....valid entries would be 100%
or
100.....need help with being able to catch entries like rree or rr%

any help much appreciated.

regards,
Amit



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Data Validation for % input in userfom

I agree.
--
Gary''s Student - gsnu200785


"Rick Rothstein (MVP - VB)" wrote:

I just noticed... you defined the lower case "L" as being equal to Len(x).
That is a bad choice for a variable name... on my system, it looked like
your wrote One minus One rather than El minus One, hence my comment.

Rick


"Rick Rothstein (MVP - VB)" wrote in
message ...
For your assignment to v2, I think you meant

v2 = Left(x, Len(x) - 1)

Rick


"Gary''s Student" wrote in
message ...
We can explicitly test the input:

Sub mesage()
x = Application.InputBox(Prompt:="Enter value:", Type:=2)
ok = False
If IsNumeric(x) Then
ok = True
Else
l = Len(x)
If l 1 Then
v1 = Right(x, 1)
v2 = Left(x, l - 1)
If v1 = "%" And IsNumeric(v2) Then
ok = True
End If
End If
End If

If ok Then
MsgBox ("data good")
Else
MsgBox ("data not valid")
End If
End Sub

We test if the data is a number or a number followed by the % character.

--
Gary''s Student - gsnu200785


"amit" wrote:

hi - I have a user entry text box to enter a % value...and a submit
command
button which mutiplies the entry with another number. i'm able to take
inputs
both as % and as whole numbers and based on the input..either remove the
% or
if whole number then divide by 100. i would like to put a change event
which
catches any input other than numers or %....valid entries would be 100%
or
100.....need help with being able to catch entries like rree or rr%

any help much appreciated.

regards,
Amit






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 811
Default Data Validation for % input in userfom

Hi Amit,

You can use the KeyPress event of the TextBox to check each character as
it's entered and either accept it or reject it. In the example below,
numbers, the % symbol and backspace are allowed, while all other characters
are cancelled.

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 8, 37, 48 To 57
''' Backspace, % and numbers are all OK
Case Else
''' Cancel all other characters.
KeyAscii = 0
End Select
End Sub

Even with this event in place you'll still need to do some additional
validation in your Submit button click event to trap for the few situations
where the user can still enter invalid data, like 12%3 or just %.

--
Rob Bovey, Excel MVP
Application Professionals
http://www.appspro.com/

* Take your Excel development skills to the next level.
* Professional Excel Development
http://www.appspro.com/Books/Books.htm

"amit" wrote in message
...
hi - I have a user entry text box to enter a % value...and a submit
command
button which mutiplies the entry with another number. i'm able to take
inputs
both as % and as whole numbers and based on the input..either remove the %
or
if whole number then divide by 100. i would like to put a change event
which
catches any input other than numers or %....valid entries would be 100% or
100.....need help with being able to catch entries like rree or rr%

any help much appreciated.

regards,
Amit



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Data Validation for % input in userfom

Just figured I would mention that users will still be able to paste in
invalid data, so a more thorough check of the entry will still be necessary.
This type of problem (restrict entry to certain characters) came up many
times over in the compiled VB newsgroups where I originally frequented a
lot. Here is a modification of the routine I used to post which handles
digits only (no decimal point, although a routine for that can be created if
needed) and an optional single trailing percent sign and which will not let
a user paste bad data in. All the following code should be copy/pasted
exactly as is into the UserForm code window.

'******************** START OF CODE ********************
Dim LastPosition As Long

Private Sub TextBox1_Change()
Static LastText As String
Static SecondTime As Boolean
If Not SecondTime Then
With TextBox1
If .Text Like "*[!0-9%]*" Or .Text Like "*%?" Or _
.Text Like "*%*%*" Or .Text = "%" Then
Beep
SecondTime = True
.Text = LastText
.SelStart = LastPosition
Else
LastText = .Text
End If
End With
End If
SecondTime = False
End Sub

Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As _
Integer, ByVal X As Single, ByVal Y As Single)
With TextBox1
LastPosition = .SelStart
'Place any other MouseDown event code here
End With
End Sub

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
With TextBox1
LastPosition = .SelStart
'Place any other KeyPress checking code here
End With
End Sub
'******************** END OF CODE ********************

Rick


"Rob Bovey" wrote in message
...
Hi Amit,

You can use the KeyPress event of the TextBox to check each character
as it's entered and either accept it or reject it. In the example below,
numbers, the % symbol and backspace are allowed, while all other
characters are cancelled.

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 8, 37, 48 To 57
''' Backspace, % and numbers are all OK
Case Else
''' Cancel all other characters.
KeyAscii = 0
End Select
End Sub

Even with this event in place you'll still need to do some additional
validation in your Submit button click event to trap for the few
situations where the user can still enter invalid data, like 12%3 or just
%.

--
Rob Bovey, Excel MVP
Application Professionals
http://www.appspro.com/

* Take your Excel development skills to the next level.
* Professional Excel Development
http://www.appspro.com/Books/Books.htm

"amit" wrote in message
...
hi - I have a user entry text box to enter a % value...and a submit
command
button which mutiplies the entry with another number. i'm able to take
inputs
both as % and as whole numbers and based on the input..either remove the
% or
if whole number then divide by 100. i would like to put a change event
which
catches any input other than numers or %....valid entries would be 100%
or
100.....need help with being able to catch entries like rree or rr%

any help much appreciated.

regards,
Amit




  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 811
Default Data Validation for % input in userfom

Hi Rick,

Good point, I'd never noticed the paste problem. My KeyPress validation
has been probably been bypassed like this plenty of times, but since I
almost always add a second layer of validation, I just never found out about
it.

--
Rob Bovey, Excel MVP
Application Professionals
http://www.appspro.com/

* Take your Excel development skills to the next level.
* Professional Excel Development
http://www.appspro.com/Books/Books.htm

"Rick Rothstein (MVP - VB)" wrote in
message ...
Just figured I would mention that users will still be able to paste in
invalid data, so a more thorough check of the entry will still be
necessary. This type of problem (restrict entry to certain characters)
came up many times over in the compiled VB newsgroups where I originally
frequented a lot. Here is a modification of the routine I used to post
which handles digits only (no decimal point, although a routine for that
can be created if needed) and an optional single trailing percent sign and
which will not let a user paste bad data in. All the following code should
be copy/pasted exactly as is into the UserForm code window.

'******************** START OF CODE ********************
Dim LastPosition As Long

Private Sub TextBox1_Change()
Static LastText As String
Static SecondTime As Boolean
If Not SecondTime Then
With TextBox1
If .Text Like "*[!0-9%]*" Or .Text Like "*%?" Or _
.Text Like "*%*%*" Or .Text = "%" Then
Beep
SecondTime = True
.Text = LastText
.SelStart = LastPosition
Else
LastText = .Text
End If
End With
End If
SecondTime = False
End Sub

Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As _
Integer, ByVal X As Single, ByVal Y As Single)
With TextBox1
LastPosition = .SelStart
'Place any other MouseDown event code here
End With
End Sub

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
With TextBox1
LastPosition = .SelStart
'Place any other KeyPress checking code here
End With
End Sub
'******************** END OF CODE ********************

Rick


"Rob Bovey" wrote in message
...
Hi Amit,

You can use the KeyPress event of the TextBox to check each character
as it's entered and either accept it or reject it. In the example below,
numbers, the % symbol and backspace are allowed, while all other
characters are cancelled.

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 8, 37, 48 To 57
''' Backspace, % and numbers are all OK
Case Else
''' Cancel all other characters.
KeyAscii = 0
End Select
End Sub

Even with this event in place you'll still need to do some additional
validation in your Submit button click event to trap for the few
situations where the user can still enter invalid data, like 12%3 or just
%.

--
Rob Bovey, Excel MVP
Application Professionals
http://www.appspro.com/

* Take your Excel development skills to the next level.
* Professional Excel Development
http://www.appspro.com/Books/Books.htm

"amit" wrote in message
...
hi - I have a user entry text box to enter a % value...and a submit
command
button which mutiplies the entry with another number. i'm able to take
inputs
both as % and as whole numbers and based on the input..either remove the
% or
if whole number then divide by 100. i would like to put a change event
which
catches any input other than numers or %....valid entries would be 100%
or
100.....need help with being able to catch entries like rree or rr%

any help much appreciated.

regards,
Amit






  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 65
Default Data Validation for % input in userfom

Thx a lot Gary, Rob and Rick...all very good suggestions.....i've bot the
methods u've suggested and i do have a second level of validation to
accomadate 2%3....

thx guys for all your responses..much appreciated...

"Rob Bovey" wrote:

Hi Rick,

Good point, I'd never noticed the paste problem. My KeyPress validation
has been probably been bypassed like this plenty of times, but since I
almost always add a second layer of validation, I just never found out about
it.

--
Rob Bovey, Excel MVP
Application Professionals
http://www.appspro.com/

* Take your Excel development skills to the next level.
* Professional Excel Development
http://www.appspro.com/Books/Books.htm

"Rick Rothstein (MVP - VB)" wrote in
message ...
Just figured I would mention that users will still be able to paste in
invalid data, so a more thorough check of the entry will still be
necessary. This type of problem (restrict entry to certain characters)
came up many times over in the compiled VB newsgroups where I originally
frequented a lot. Here is a modification of the routine I used to post
which handles digits only (no decimal point, although a routine for that
can be created if needed) and an optional single trailing percent sign and
which will not let a user paste bad data in. All the following code should
be copy/pasted exactly as is into the UserForm code window.

'******************** START OF CODE ********************
Dim LastPosition As Long

Private Sub TextBox1_Change()
Static LastText As String
Static SecondTime As Boolean
If Not SecondTime Then
With TextBox1
If .Text Like "*[!0-9%]*" Or .Text Like "*%?" Or _
.Text Like "*%*%*" Or .Text = "%" Then
Beep
SecondTime = True
.Text = LastText
.SelStart = LastPosition
Else
LastText = .Text
End If
End With
End If
SecondTime = False
End Sub

Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As _
Integer, ByVal X As Single, ByVal Y As Single)
With TextBox1
LastPosition = .SelStart
'Place any other MouseDown event code here
End With
End Sub

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
With TextBox1
LastPosition = .SelStart
'Place any other KeyPress checking code here
End With
End Sub
'******************** END OF CODE ********************

Rick


"Rob Bovey" wrote in message
...
Hi Amit,

You can use the KeyPress event of the TextBox to check each character
as it's entered and either accept it or reject it. In the example below,
numbers, the % symbol and backspace are allowed, while all other
characters are cancelled.

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 8, 37, 48 To 57
''' Backspace, % and numbers are all OK
Case Else
''' Cancel all other characters.
KeyAscii = 0
End Select
End Sub

Even with this event in place you'll still need to do some additional
validation in your Submit button click event to trap for the few
situations where the user can still enter invalid data, like 12%3 or just
%.

--
Rob Bovey, Excel MVP
Application Professionals
http://www.appspro.com/

* Take your Excel development skills to the next level.
* Professional Excel Development
http://www.appspro.com/Books/Books.htm

"amit" wrote in message
...
hi - I have a user entry text box to enter a % value...and a submit
command
button which mutiplies the entry with another number. i'm able to take
inputs
both as % and as whole numbers and based on the input..either remove the
% or
if whole number then divide by 100. i would like to put a change event
which
catches any input other than numers or %....valid entries would be 100%
or
100.....need help with being able to catch entries like rree or rr%

any help much appreciated.

regards,
Amit






  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Data Validation for % input in userfom

I'd be interested in seeing your 2nd level of validation code.

Rick


"amit" wrote in message
...
Thx a lot Gary, Rob and Rick...all very good suggestions.....i've bot the
methods u've suggested and i do have a second level of validation to
accomadate 2%3....

thx guys for all your responses..much appreciated...

"Rob Bovey" wrote:

Hi Rick,

Good point, I'd never noticed the paste problem. My KeyPress
validation
has been probably been bypassed like this plenty of times, but since I
almost always add a second layer of validation, I just never found out
about
it.

--
Rob Bovey, Excel MVP
Application Professionals
http://www.appspro.com/

* Take your Excel development skills to the next level.
* Professional Excel Development
http://www.appspro.com/Books/Books.htm

"Rick Rothstein (MVP - VB)" wrote
in
message ...
Just figured I would mention that users will still be able to paste in
invalid data, so a more thorough check of the entry will still be
necessary. This type of problem (restrict entry to certain characters)
came up many times over in the compiled VB newsgroups where I
originally
frequented a lot. Here is a modification of the routine I used to post
which handles digits only (no decimal point, although a routine for
that
can be created if needed) and an optional single trailing percent sign
and
which will not let a user paste bad data in. All the following code
should
be copy/pasted exactly as is into the UserForm code window.

'******************** START OF CODE ********************
Dim LastPosition As Long

Private Sub TextBox1_Change()
Static LastText As String
Static SecondTime As Boolean
If Not SecondTime Then
With TextBox1
If .Text Like "*[!0-9%]*" Or .Text Like "*%?" Or _
.Text Like "*%*%*" Or .Text = "%" Then
Beep
SecondTime = True
.Text = LastText
.SelStart = LastPosition
Else
LastText = .Text
End If
End With
End If
SecondTime = False
End Sub

Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As
_
Integer, ByVal X As Single, ByVal Y As Single)
With TextBox1
LastPosition = .SelStart
'Place any other MouseDown event code here
End With
End Sub

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
With TextBox1
LastPosition = .SelStart
'Place any other KeyPress checking code here
End With
End Sub
'******************** END OF CODE ********************

Rick


"Rob Bovey" wrote in message
...
Hi Amit,

You can use the KeyPress event of the TextBox to check each
character
as it's entered and either accept it or reject it. In the example
below,
numbers, the % symbol and backspace are allowed, while all other
characters are cancelled.

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 8, 37, 48 To 57
''' Backspace, % and numbers are all OK
Case Else
''' Cancel all other characters.
KeyAscii = 0
End Select
End Sub

Even with this event in place you'll still need to do some
additional
validation in your Submit button click event to trap for the few
situations where the user can still enter invalid data, like 12%3 or
just
%.

--
Rob Bovey, Excel MVP
Application Professionals
http://www.appspro.com/

* Take your Excel development skills to the next level.
* Professional Excel Development
http://www.appspro.com/Books/Books.htm

"amit" wrote in message
...
hi - I have a user entry text box to enter a % value...and a submit
command
button which mutiplies the entry with another number. i'm able to
take
inputs
both as % and as whole numbers and based on the input..either remove
the
% or
if whole number then divide by 100. i would like to put a change
event
which
catches any input other than numers or %....valid entries would be
100%
or
100.....need help with being able to catch entries like rree or rr%

any help much appreciated.

regards,
Amit









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
Data Input Validation Mark S[_2_] Excel Discussion (Misc queries) 5 February 24th 08 12:00 AM
Input Box data validation Mathew Excel Programming 7 September 6th 07 08:09 PM
Data Validation list in Input Box... Possible ? Corey Excel Programming 5 June 28th 06 08:00 AM
DataValidationInput message irresistible007 Excel Worksheet Functions 1 December 3rd 05 01:06 PM
Data Validation with additional input Shane73 Excel Programming 2 October 17th 05 06:59 AM


All times are GMT +1. The time now is 10:27 PM.

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

About Us

"It's about Microsoft Excel"