Thread: Array problem
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Bob Phillips[_3_] Bob Phillips[_3_] is offline
external usenet poster
 
Posts: 2,420
Default Array problem

Try this

Sub ValidateStaffStatus()
'validates the task codes of the amended staff

Dim c As Range
Dim There As Boolean
Dim ErrCount As Integer
Dim i As Variant
Dim ValidationArray() As Variant
ValidationArray() = Application.Transpose( _
ThisWorkbook.Sheets("All Shifts").Range("B3:B41").Value)

ThisWorkbook.Sheets("AWD").Select
ThisWorkbook.Sheets("AWD").Range("E2:L1001").Clear Formats
For Each c In Range("E2:E10")
If Not c.Value = "" Then 'the cell is not empty
If Not IsNumeric(c.Value) Then 'the cell value is not numeric
If Not c.Value = "NWD" Then 'not a non working day
'validate the status codes

If Not IsError(Application.Match(c.Value,
ValidationArray, 0)) Then
c.Interior.ColorIndex = 3
ErrCount = ErrCount + 1
End If
End If
End If
End If
Next c

If ErrCount = 0 Then
MsgBox "No errors have been found"
Else
MsgBox "Your data contains " & ErrCount & _
" error(s). These have been highlighted for correction."
End If

End Sub


--
__________________________________
HTH

Bob

"MJKelly" wrote in message
...
Hi,
I'm having problems with the code below. I want to create an array
from a range. This array will hold the codes for different tasks.
Then i need to loop through a range and highlight any codes which are
ont contained in the array. I can do this if I manually fill the
array using code, but this takes too long and would need constant
adjustments. So filling the array from a range is far better for me.
When I run this code is stops on this line:

If c.Value = ValidationArray(i) Then
I get a subscript out of range error.

also how do I declare "i"? I've just used Dim i as variant for now

Hope you can help.
regards,
Matt


Sub ValidateStaffStatus()
'validates the task codes of the amended staff

Dim c As Range
Dim There As Boolean
Dim ErrCount As Integer
Dim i as variant
Dim ValidationArray() As Variant
ValidationArray() = ThisWorkbook.Sheets("All Shifts").Range
("B3:B41").Value

ThisWorkbook.Sheets("AWD").Select
ThisWorkbook.Sheets("AWD").Range("E2:L1001").Clear Formats
For Each c In Range("E2:E10")
If Not c.Value = "" Then 'the cell is not empty
If Not IsNumeric(c.Value) Then 'the cell value is not numeric
If Not c.Value = "NWD" Then 'not a non working day
'validate the status codes
There = False
For i = 0 To UBound(ValidationArray)
If c.Value = ValidationArray(i) Then
There = True
Exit For
End If
Next

If There = False Then
c.Interior.ColorIndex = 3
ErrCount = ErrCount + 1
End If
End If
End If
End If
Next c

If ErrCount = 0 Then
MsgBox "No errors have been found"
Else
MsgBox "Your data contains " & ErrCount & " error(s). These have been
highlighted for correction."
End If

End Sub