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

Good Morning All...Your help is greatly appreciated!

I am in the process of setting up the Data Validation using Custom option to
validate if the right zip code is entered. the user has the option to either
enter 5 or 9 digit zip code with the option to enter either space or "-" .
here is my code

Function isValidZip(zip As String) As Boolean
Dim Punct, TempZIP As String
If InStr(1, zip, " ") 1 Then
Punct = " "
ElseIf InStr(1, zip, "-") 1 Then
Punct = "-"
End If
If InStr(1, zip, Punct) 1 Then
TempZIP = Left(zip, InStr(1, zip, Punct) - 1)
TempZIP = TempZIP & Right(zip, Len(zip) - InStr(1, zip, Punct))
zip = TempZIP
End If
If Len(zip) = 5 Or Len(zip) = 9 Then
isValidZip = True
Else
isValidZip = False
End If
If Not CInt(zip) Then
MsgBox "Invalid Zip code, Please Try again "
End If
End Function

the problem is I want to check if what they entered is a number or not, if
not show error message. As shown below
If Not CInt(TempZIP) Then
MsgBox "Invalid Zip code, Please Try again "
End If
the CInt returns application error or type mismatch
is there other type of function that i can use or create to check if data
enterd is a number and if not then show error message ?

thank you
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,117
Default Check Format

you could use the Not IsNumeric concept..........

If Not IsNumeric(TempZIP) Then
MsgBox "Invalid Zip code, Please Try again "
End If


if this doesn't work exactly, try searching
the newsgroup for "Not IsNumeric" for help
with this.
susan

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 29
Default Check Format

Thank you very much this works!

"Susan" wrote:

you could use the Not IsNumeric concept..........

If Not IsNumeric(TempZIP) Then
MsgBox "Invalid Zip code, Please Try again "
End If


if this doesn't work exactly, try searching
the newsgroup for "Not IsNumeric" for help
with this.
susan


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 44
Default Check Format

You could also use a pattern search ...

say zip is your string...

something like :
if not (zip like "#####" or _
zip like "#####-####") then

msgbox "Invalid format for zip code", vbinformation
' whatever you want to do ...
end if

what you're telling the VBA compiler - look for an exact match "#####" which
is 5 numbers together or "#####-####" which means 5 numbers a '-' character,
and 4 more numbers ... can modify that to meet your criterion...

hope that helps,
Chad

"Student" wrote:

Good Morning All...Your help is greatly appreciated!

I am in the process of setting up the Data Validation using Custom option to
validate if the right zip code is entered. the user has the option to either
enter 5 or 9 digit zip code with the option to enter either space or "-" .
here is my code

Function isValidZip(zip As String) As Boolean
Dim Punct, TempZIP As String
If InStr(1, zip, " ") 1 Then
Punct = " "
ElseIf InStr(1, zip, "-") 1 Then
Punct = "-"
End If
If InStr(1, zip, Punct) 1 Then
TempZIP = Left(zip, InStr(1, zip, Punct) - 1)
TempZIP = TempZIP & Right(zip, Len(zip) - InStr(1, zip, Punct))
zip = TempZIP
End If
If Len(zip) = 5 Or Len(zip) = 9 Then
isValidZip = True
Else
isValidZip = False
End If
If Not CInt(zip) Then
MsgBox "Invalid Zip code, Please Try again "
End If
End Function

the problem is I want to check if what they entered is a number or not, if
not show error message. As shown below
If Not CInt(TempZIP) Then
MsgBox "Invalid Zip code, Please Try again "
End If
the CInt returns application error or type mismatch
is there other type of function that i can use or create to check if data
enterd is a number and if not then show error message ?

thank you

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
Check if Conditional Format is True or False / Check cell Color Kevin McCartney Excel Worksheet Functions 5 June 29th 07 11:12 AM
Check box format AnthonyJ Excel Programming 1 October 12th 06 02:34 PM
Cell Format Check Rich[_30_] Excel Programming 3 May 17th 06 04:54 PM
Check my Conditional Format Formula D[_6_] Excel Programming 3 August 11th 04 05:34 PM
Check Box Format! Kim-Anh Tran Excel Programming 5 May 3rd 04 11:01 PM


All times are GMT +1. The time now is 05:12 AM.

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"