Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Function to validate entries in a defined range

I want to create a function that tests a range of cells stated to ensure
that the cells contain only valid data entries - lets call the function
ValidateEntry.

For my purposes valid data is any cell in the range whose value is either:-

1) Null (Blank or empty)
2) "X" (the letter X)
3) Begins with the letters "DC" and is immediately followed by 2 digits e.g
DC09
4) Begins with the letters "SC" and is immediately followed by 2 digits e.g
SC25


e.g ValidateEntry(A5:Z5) would return a "true" statues if all cells within
that range met the criteria above, if not then it would return a "false"
answer (I.e boolean argument).

I want to then use this function in an If statement
e.g If(ValidateEntry(A5:Z5),"Hooray!","Ooops!")

Sorry - still learning!!!


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Function to validate entries in a defined range

Function ValidateEntry(rng As Range) As Boolean
Dim cell As Range
ValidateEntry = True
For Each cell In rng
If (IsEmpty(cell.Value) Or _
cell.Value = "" Or _
cell.Value = "X" Or _
(Len(cell.Value) 3 And Left(cell.Value, 2) = "DC" And _
IsNumeric(Mid(cell.Value, 3, 1)) And IsNumeric(Mid(cell.Value,
4, 1))) Or _
(Len(cell.Value) 3 And Left(cell.Value, 2) = "SC" And _
IsNumeric(Mid(cell.Value, 3, 1)) And IsNumeric(Mid(cell.Value,
4, 1)))) Then
Else
ValidateEntry = False
Exit Function
End If
Next cell
End Function


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Lee Wold" wrote in message
...
I want to create a function that tests a range of cells stated to ensure
that the cells contain only valid data entries - lets call the function
ValidateEntry.

For my purposes valid data is any cell in the range whose value is

either:-

1) Null (Blank or empty)
2) "X" (the letter X)
3) Begins with the letters "DC" and is immediately followed by 2 digits

e.g
DC09
4) Begins with the letters "SC" and is immediately followed by 2 digits

e.g
SC25


e.g ValidateEntry(A5:Z5) would return a "true" statues if all cells within
that range met the criteria above, if not then it would return a "false"
answer (I.e boolean argument).

I want to then use this function in an If statement
e.g If(ValidateEntry(A5:Z5),"Hooray!","Ooops!")

Sorry - still learning!!!




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Function to validate entries in a defined range

Cheers Bob,

It worked a treat.

I just changed the Len function's to "= 4" to force a format of SC or DC
with 2 digits e.g. SC01 ok but SC111 not ok (for my purposes all valid SC or
DC entries would always be followd by 2 digits exactly).

Thanks again.




"Bob Phillips" wrote in message
...
Function ValidateEntry(rng As Range) As Boolean
Dim cell As Range
ValidateEntry = True
For Each cell In rng
If (IsEmpty(cell.Value) Or _
cell.Value = "" Or _
cell.Value = "X" Or _
(Len(cell.Value) 3 And Left(cell.Value, 2) = "DC" And _
IsNumeric(Mid(cell.Value, 3, 1)) And IsNumeric(Mid(cell.Value,
4, 1))) Or _
(Len(cell.Value) 3 And Left(cell.Value, 2) = "SC" And _
IsNumeric(Mid(cell.Value, 3, 1)) And IsNumeric(Mid(cell.Value,
4, 1)))) Then
Else
ValidateEntry = False
Exit Function
End If
Next cell
End Function


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Lee Wold" wrote in message
...
I want to create a function that tests a range of cells stated to ensure
that the cells contain only valid data entries - lets call the function
ValidateEntry.

For my purposes valid data is any cell in the range whose value is

either:-

1) Null (Blank or empty)
2) "X" (the letter X)
3) Begins with the letters "DC" and is immediately followed by 2 digits

e.g
DC09
4) Begins with the letters "SC" and is immediately followed by 2 digits

e.g
SC25


e.g ValidateEntry(A5:Z5) would return a "true" statues if all cells
within
that range met the criteria above, if not then it would return a "false"
answer (I.e boolean argument).

I want to then use this function in an If statement
e.g If(ValidateEntry(A5:Z5),"Hooray!","Ooops!")

Sorry - still learning!!!






  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Function to validate entries in a defined range

Hi Lee,

I wondered about that. Glad you cracked it.

Bob


"Lee Wold" wrote in message
...
Cheers Bob,

It worked a treat.

I just changed the Len function's to "= 4" to force a format of SC or DC
with 2 digits e.g. SC01 ok but SC111 not ok (for my purposes all valid SC

or
DC entries would always be followd by 2 digits exactly).

Thanks again.




"Bob Phillips" wrote in message
...
Function ValidateEntry(rng As Range) As Boolean
Dim cell As Range
ValidateEntry = True
For Each cell In rng
If (IsEmpty(cell.Value) Or _
cell.Value = "" Or _
cell.Value = "X" Or _
(Len(cell.Value) 3 And Left(cell.Value, 2) = "DC" And _
IsNumeric(Mid(cell.Value, 3, 1)) And

IsNumeric(Mid(cell.Value,
4, 1))) Or _
(Len(cell.Value) 3 And Left(cell.Value, 2) = "SC" And _
IsNumeric(Mid(cell.Value, 3, 1)) And

IsNumeric(Mid(cell.Value,
4, 1)))) Then
Else
ValidateEntry = False
Exit Function
End If
Next cell
End Function


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Lee Wold" wrote in message
...
I want to create a function that tests a range of cells stated to

ensure
that the cells contain only valid data entries - lets call the function
ValidateEntry.

For my purposes valid data is any cell in the range whose value is

either:-

1) Null (Blank or empty)
2) "X" (the letter X)
3) Begins with the letters "DC" and is immediately followed by 2 digits

e.g
DC09
4) Begins with the letters "SC" and is immediately followed by 2 digits

e.g
SC25


e.g ValidateEntry(A5:Z5) would return a "true" statues if all cells
within
that range met the criteria above, if not then it would return a

"false"
answer (I.e boolean argument).

I want to then use this function in an If statement
e.g If(ValidateEntry(A5:Z5),"Hooray!","Ooops!")

Sorry - still learning!!!








  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default Function to validate entries in a defined range

Bob and you have already solved your specific problem, but you might
want to consider the following.

Since the validation function works across a range of cells, it would
be helpful to indicate which cell(s) in the range are unacceptable.
Towards that end, consider using XL's data validation or conditional
formatting capability. If you must use a UDF in the manner discussed
so far, have it return the cells in error. Yes, that means it cannot
return a boolean, but the result would be far more useful to your
user/customer.

Option Explicit

Function ValidateEntry(rng As Range) As String
Dim Cell As Range
For Each Cell In rng
If IsEmpty(Cell.Value) Or _
Cell.Value = "" Or _
Cell.Value = "X" Or _
(Len(Cell.Value) = 4 _
And (UCase(Left(Cell.Value, 2)) = "DC" _
Or UCase(Left(Cell.Value, 2)) = "SC") _
And IsNumeric(Mid(Cell.Value, 3, 1)) _
And IsNumeric(Mid(Cell.Value, 4, 1))) Then
Else
ValidateEntry = ValidateEntry & Cell.Address & ","
End If
Next Cell
If ValidateEntry = "" Then
ValidateEntry = "OK"
Else
ValidateEntry = "Errors in " _
& Left(ValidateEntry, Len(ValidateEntry) - 1)
End If
End Function

Also, note the consolidation of the DC and SC checks into a subordinate
OR clause and the use of UCase to support user entry in lower or upper
case letters.

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
Cheers Bob,

It worked a treat.

I just changed the Len function's to "= 4" to force a format of SC or DC
with 2 digits e.g. SC01 ok but SC111 not ok (for my purposes all valid SC or
DC entries would always be followd by 2 digits exactly).

Thanks again.




"Bob Phillips" wrote in message
...
Function ValidateEntry(rng As Range) As Boolean
Dim cell As Range
ValidateEntry = True
For Each cell In rng
If (IsEmpty(cell.Value) Or _
cell.Value = "" Or _
cell.Value = "X" Or _
(Len(cell.Value) 3 And Left(cell.Value, 2) = "DC" And _
IsNumeric(Mid(cell.Value, 3, 1)) And IsNumeric(Mid(cell.Value,
4, 1))) Or _
(Len(cell.Value) 3 And Left(cell.Value, 2) = "SC" And _
IsNumeric(Mid(cell.Value, 3, 1)) And IsNumeric(Mid(cell.Value,
4, 1)))) Then
Else
ValidateEntry = False
Exit Function
End If
Next cell
End Function


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Lee Wold" wrote in message
...
I want to create a function that tests a range of cells stated to ensure
that the cells contain only valid data entries - lets call the function
ValidateEntry.

For my purposes valid data is any cell in the range whose value is

either:-

1) Null (Blank or empty)
2) "X" (the letter X)
3) Begins with the letters "DC" and is immediately followed by 2 digits

e.g
DC09
4) Begins with the letters "SC" and is immediately followed by 2 digits

e.g
SC25


e.g ValidateEntry(A5:Z5) would return a "true" statues if all cells
within
that range met the criteria above, if not then it would return a "false"
answer (I.e boolean argument).

I want to then use this function in an If statement
e.g If(ValidateEntry(A5:Z5),"Hooray!","Ooops!")

Sorry - still learning!!!







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
Inserting a row within a range that is defined in a sum function Victor[_2_] Excel Discussion (Misc queries) 2 November 25th 09 04:32 PM
validate time entries linked to each other matfis Excel Worksheet Functions 2 October 19th 09 08:46 PM
Possible to validate entries not to contain 00 [email protected] Excel Discussion (Misc queries) 1 December 14th 07 09:55 AM
How to validate entries with Data Forms in xls 2003? Marc S. Setting up and Configuration of Excel 1 May 25th 06 03:19 PM
Validate entries Frank Kabel Excel Programming 0 September 13th 04 05:49 PM


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