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


Hi,
The code below checks a range for errors. I want to build on the line
which checks the value for "AL" or "SL" to check for some 30 different
possibilities. Is there a better way of doing this than Xor c.value =
"Pm" Xor c.value = "XD" etc. Like I say this will be around 30
different possibilities.

I tried
if c.value < ("AL", "SL", "PM") then
do something
end if

but this doesnt work. Is there a syntax I can use other than a long
winded Xor?

Sub Validate_Status()
'checks for errors in status names entries

Dim ErrCount
Dim c As Range

ThisWorkbook.Sheets("Sheet1").Select
Range("A1:A10").ClearFormats

For Each c In Range("A1:A10")
If Not c.Value = "" Then 'the cell is not empty
If Not IsNumeric(c.Value) Then 'the cell value is not numeric
'validate the staus codes
If Not c.Value = "AL" Xor c.Value = "SL" Then
c.Interior.ColorIndex = 3
ErrCount = ErrCount + 1
End If
End If
End If
Next c

MsgBox ErrCount

End Sub

Kind regards,
Matt
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default multiple validation

Hi,


Validate against an array. Note at the beginning of your code I've created
an array of values to validate against. Extend this to your full list. In the
midddle of the code there's an If loop that validates against each element of
the array

Sub Validate_Status()
'checks for errors in status names entries
Dim MyArray()
Dim There As Boolean
Dim ErrCount
Dim c As Range
MyArray = Array("AL", "SL", "BT", "PP")
ThisWorkbook.Sheets("Sheet1").Select
Range("A1:A10").ClearFormats

For Each c In Range("A1:A10")
If Not c.Value = "" Then 'the cell is not empty
If Not IsNumeric(c.Value) Then 'the cell value is not numeric
'validate the staus codes

There = False
For i = 0 To UBound(MyArray)
If c.Value = MyArray(i) Then
There = True
Exit For
End If
Next
c.Interior.ColorIndex = 3
If There = False Then ErrCount = ErrCount + 1

End If
End If
Next c
MsgBox ErrCount
End Sub


Mike


"MJKelly" wrote:


Hi,
The code below checks a range for errors. I want to build on the line
which checks the value for "AL" or "SL" to check for some 30 different
possibilities. Is there a better way of doing this than Xor c.value =
"Pm" Xor c.value = "XD" etc. Like I say this will be around 30
different possibilities.

I tried
if c.value < ("AL", "SL", "PM") then
do something
end if

but this doesnt work. Is there a syntax I can use other than a long
winded Xor?

Sub Validate_Status()
'checks for errors in status names entries

Dim ErrCount
Dim c As Range

ThisWorkbook.Sheets("Sheet1").Select
Range("A1:A10").ClearFormats

For Each c In Range("A1:A10")
If Not c.Value = "" Then 'the cell is not empty
If Not IsNumeric(c.Value) Then 'the cell value is not numeric
'validate the staus codes
If Not c.Value = "AL" Xor c.Value = "SL" Then
c.Interior.ColorIndex = 3
ErrCount = ErrCount + 1
End If
End If
End If
Next c

MsgBox ErrCount

End Sub

Kind regards,
Matt

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 114
Default multiple validation

Mike, that's excellent, works a treat. One further benefit for me
would be for the array to be populated with data from a range. Can
this be done?

Thanks again,
Matt
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default multiple validation

Hi,

I've done the code seperately so you can see what's happening. The
debub.print statement is to show you the elements being printed to the
immediate window and isn't necessary in your final code. In this case the
array elements ar in column A of sheet 2.


Sub Sonic()
Dim MyArray()
lastrow = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
ReDim MyArray(1 To lastrow)
For x = 1 To lastrow
MyArray(x) = Sheets("Sheet2").Cells(x, 1).Value
Debug.Print MyArray(x)
Next
End Sub

Mike

"MJKelly" wrote:

Mike, that's excellent, works a treat. One further benefit for me
would be for the array to be populated with data from a range. Can
this be done?

Thanks again,
Matt

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 114
Default multiple validation

Mike,
Great stuff - the final piece of the puzzle.

best regards,
Matt
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
multiple data validation Fiver Excel Discussion (Misc queries) 3 July 26th 09 04:41 PM
Multiple Data Validation JStiehl Excel Discussion (Misc queries) 7 July 1st 09 04:50 PM
Multiple Data Validation betany70 Excel Discussion (Misc queries) 2 July 24th 07 09:42 PM
Validation of multiple selections Mark Excel Programming 2 May 9th 07 09:37 PM
Multiple Validation Lists Alison Excel Worksheet Functions 2 July 28th 06 01:51 AM


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