Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 333
Default Validating with Like

Hi,

I've been trying to validate the value of a textbox with partial success.
The value input must be a number but it can also be a decimal number. I am
trying to catch alpha characters but my Like statement is not catching it. I
have tried various versions, "A-Z", "a' - "z", a-z.

Sub EditAllowedVariance(PasswordGood)

Dim NuAllowance As String

If PasswordGood = True Then
NuAllowance = InputBox("Enter desired allowed variance", _

"Enter Variance")
If NuAllowance Like ["a-z"] Then
MsgBox "Variance must be a number"
Goto InvalidNo
End If
If Val(NuAllowance) < 0 Then
MsgBox "Allowance must be positive number"
GoTo InvalidNo
Else
GrossUppg.Range("I2").NumberFormat = "00.00"
GrossUppg.Range("I2").Value = Val(NuAllowance)
End If
End If

InvalidNo:

End Sub
--
Thanks for your help.
Karen53
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Validating with Like

You may find this function useful, which I use to remove non-numeric
characters from a textbox as they are typed:

Function RemoveNonNumeric2(strString As String, _
Optional bIntegerOnly As Boolean, _
Optional bPositiveOnly As Boolean, _
Optional bClearAndOut As Boolean, _
Optional bHasChanged As Boolean) As String

Dim i As Long
Dim c As Long
Dim bHasDot As Boolean
Dim btArray() As Byte

'45 -
'46 .
'48 to 58 0 to 9
'------------------

btArray = strString

If bIntegerOnly Then
If bPositiveOnly Then
For i = 0 To UBound(btArray) Step 2
If btArray(i) < 48 Or btArray(i) 58 Then
bHasChanged = True
If bClearAndOut Then
RemoveNonNumeric2 = vbNullString
Exit Function
End If
strString = Left$(strString, i \ 2 - c) & _
Mid$(strString, i \ 2 + 2 - c)
c = c + 1
End If
Next i
Else 'If bPositiveOnly
For i = 0 To UBound(btArray) Step 2
If btArray(i) < 48 Or btArray(i) 58 Then
If i - c * 2 = 0 Then
If btArray(i) < 45 Then
bHasChanged = True
If bClearAndOut Then
RemoveNonNumeric2 = vbNullString
Exit Function
End If
strString = Left$(strString, i \ 2 - c) & _
Mid$(strString, i \ 2 + 2 - c)
c = c + 1
End If
Else 'If i - c * 2 = 0
bHasChanged = True
If bClearAndOut Then
RemoveNonNumeric2 = ""
Exit Function
End If
strString = Left$(strString, i \ 2 - c) & _
Mid$(strString, i \ 2 + 2 - c)
c = c + 1
End If 'If i - c * 2 = 0
End If
Next i
End If 'If bPositiveOnly
Else 'If bIntegerOnly
If bPositiveOnly Then
For i = 0 To UBound(btArray) Step 2
If btArray(i) < 48 Or btArray(i) 58 Then
If btArray(i) < 46 Or bHasDot Then
bHasChanged = True
If bClearAndOut Then
RemoveNonNumeric2 = vbNullString
Exit Function
End If
strString = Left$(strString, i \ 2 - c) & _
Mid$(strString, i \ 2 + 2 - c)
c = c + 1
Else
bHasDot = True
End If
End If
Next i
Else 'If bPositiveOnly
For i = 0 To UBound(btArray) Step 2
If i - c * 2 = 0 Then
If btArray(i) < 48 Or btArray(i) 58 Then
If btArray(i) < 45 Then
If btArray(i) < 46 Or bHasDot Then
bHasChanged = True
If bClearAndOut Then
RemoveNonNumeric2 = vbNullString
Exit Function
End If
strString = Left$(strString, i \ 2 - c) & _
Mid$(strString, i \ 2 + 2 - c)
c = c + 1
Else
bHasDot = True
End If
End If
End If
Else 'If i - c * 2 = 0
If btArray(i) < 48 Or btArray(i) 58 Then
If btArray(i) < 46 Or bHasDot Then
bHasChanged = True
If bClearAndOut Then
RemoveNonNumeric2 = vbNullString
Exit Function
End If
strString = Left$(strString, i \ 2 - c) & _
Mid$(strString, i \ 2 + 2 - c)
c = c + 1
Else
bHasDot = True
End If
End If
End If 'If i - c * 2 = 0
Next i
End If 'If bPositiveOnly
End If 'If bIntegerOnly

RemoveNonNumeric2 = strString

End Function


RBS



"Karen53" wrote in message
...
Hi,

I've been trying to validate the value of a textbox with partial success.
The value input must be a number but it can also be a decimal number. I
am
trying to catch alpha characters but my Like statement is not catching it.
I
have tried various versions, "A-Z", "a' - "z", a-z.

Sub EditAllowedVariance(PasswordGood)

Dim NuAllowance As String

If PasswordGood = True Then
NuAllowance = InputBox("Enter desired allowed variance", _

"Enter Variance")
If NuAllowance Like ["a-z"] Then
MsgBox "Variance must be a number"
Goto InvalidNo
End If
If Val(NuAllowance) < 0 Then
MsgBox "Allowance must be positive number"
GoTo InvalidNo
Else
GrossUppg.Range("I2").NumberFormat = "00.00"
GrossUppg.Range("I2").Value = Val(NuAllowance)
End If
End If

InvalidNo:

End Sub
--
Thanks for your help.
Karen53


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default Validating with Like

Here are two functions that I have posted in the past (over in the compiled
VB newsgroups, but they work fine in VBA)... one is for digits only and the
other is for "regular" numbers:

Function IsDigitsOnly(Value As String) As Boolean
IsDigitsOnly = Len(Value) 0 And _
Not Value Like "*[!0-9]*"
End Function

Function IsNumber(ByVal Value As String) As Boolean
' Leave the next statement out if you don't
' want to provide for plus/minus signs
If Value Like "[+-]*" Then Value = Mid$(Value, 2)
IsNumber = Not Value Like "*[!0-9.]*" And _
Not Value Like "*.*.*" And _
Len(Value) 0 And Value < "." And _
Value < vbNullString
End Function

Here are revisions to the above functions that deal with the local settings
for decimal points (and thousand's separators) that are different than used
in the US (this code works in the US too, of course).

Function IsNumber(ByVal Value As String) As Boolean
Dim DP As String
' Get local setting for decimal point
DP = Format$(0, ".")
' Leave the next statement out if you don't
' want to provide for plus/minus signs
If Value Like "[+-]*" Then Value = Mid$(Value, 2)
IsNumber = Not Value Like "*[!0-9" & DP & "]*" And _
Not Value Like "*" & DP & "*" & DP & "*" And _
Len(Value) 0 And Value < DP And _
Value < vbNullString
End Function

I'm not as concerned by the rejection of entries that include one or more
thousand's separators, but we can handle this if we don't insist on the
thousand's separator being located in the correct positions (in other words,
we'll allow the user to include them for their own purposes... we'll just
tolerate their presence).

Function IsNumber(ByVal Value As String) As Boolean
Dim DP As String
Dim TS As String
' Get local setting for decimal point
DP = Format$(0, ".")
' Get local setting for thousand's separator
' and eliminate them. Remove the next two lines
' if you don't want your users being able to
' type in the thousands separator at all.
TS = Mid$(Format$(1000, "#,###"), 2, 1)
Value = Replace$(Value, TS, "")
' Leave the next statement out if you don't
' want to provide for plus/minus signs
If Value Like "[+-]*" Then Value = Mid$(Value, 2)
IsNumber = Not Value Like "*[!0-9" & DP & "]*" And _
Not Value Like "*" & DP & "*" & DP & "*" And _
Len(Value) 0 And Value < DP And _
Value < vbNullString
End Function

Rick


"Karen53" wrote in message
...
Hi,

I've been trying to validate the value of a textbox with partial success.
The value input must be a number but it can also be a decimal number. I
am
trying to catch alpha characters but my Like statement is not catching it.
I
have tried various versions, "A-Z", "a' - "z", a-z.

Sub EditAllowedVariance(PasswordGood)

Dim NuAllowance As String

If PasswordGood = True Then
NuAllowance = InputBox("Enter desired allowed variance", _

"Enter Variance")
If NuAllowance Like ["a-z"] Then
MsgBox "Variance must be a number"
Goto InvalidNo
End If
If Val(NuAllowance) < 0 Then
MsgBox "Allowance must be positive number"
GoTo InvalidNo
Else
GrossUppg.Range("I2").NumberFormat = "00.00"
GrossUppg.Range("I2").Value = Val(NuAllowance)
End If
End If

InvalidNo:

End Sub
--
Thanks for your help.
Karen53


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 333
Default Validating with Like

Thank you, both!
--
Thanks for your help.
Karen53


"Karen53" wrote:

Hi,

I've been trying to validate the value of a textbox with partial success.
The value input must be a number but it can also be a decimal number. I am
trying to catch alpha characters but my Like statement is not catching it. I
have tried various versions, "A-Z", "a' - "z", a-z.

Sub EditAllowedVariance(PasswordGood)

Dim NuAllowance As String

If PasswordGood = True Then
NuAllowance = InputBox("Enter desired allowed variance", _

"Enter Variance")
If NuAllowance Like ["a-z"] Then
MsgBox "Variance must be a number"
Goto InvalidNo
End If
If Val(NuAllowance) < 0 Then
MsgBox "Allowance must be positive number"
GoTo InvalidNo
Else
GrossUppg.Range("I2").NumberFormat = "00.00"
GrossUppg.Range("I2").Value = Val(NuAllowance)
End If
End If

InvalidNo:

End Sub
--
Thanks for your help.
Karen53

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
Validating cells [email protected] Excel Discussion (Misc queries) 1 October 30th 08 12:53 AM
validating Philashley Excel Discussion (Misc queries) 2 March 13th 08 03:54 PM
validating Ayesha Excel Worksheet Functions 2 April 26th 06 02:56 PM
Validating Sdbenn90 Excel Discussion (Misc queries) 0 March 27th 06 12:05 AM
Validating in VBA Casey C Excel Programming 7 November 25th 05 08:22 PM


All times are GMT +1. The time now is 10:02 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"