Here's my function that isn't working:
For some reason, I never get past the Union line. I thought it was because my range was empty, but my workaround using a binary flag didn't work either.
I am trying to sort through RangeOfCells, and return an address that represents all cells but the ones that are colored the integer ColorIndex.
The color detection works, and my inputs seem fine, but I can't add the non colored cells to the "empty" array.
HELP!
For some reason, I never get past the Union line. I thought it was because my range was empty, but my workaround using a binary flag didn't work either.
I am trying to sort through RangeOfCells, and return an address that represents all cells but the ones that are colored the integer ColorIndex.
The color detection works, and my inputs seem fine, but I can't add the non colored cells to the "empty" array.
HELP!
Code:
Function ExcludeColoredCellRange(RangeOfCells As Range, ColorIndex As Long)
'returns a subrange of cells within RangeOfCells that have the background color ColorIndex
'RangeOfCells must be one column for routine to work correctly
Dim N As Long
Dim MyRange As Range
' Dim flag As Long 'workaround to empty array problem
' flag = 0
MsgBox ("Range Input: " & RangeOfCells.Address(ReferenceStyle:=xlA1)) 'debug
MsgBox ("Length: " & RangeOfCells.Count()) 'debug
For N = 1 To RangeOfCells.Rows.Count
MsgBox ("N = " & N) 'debug
If Not (RangeOfCells.Cells(N, 1).Interior.ColorIndex = ColorIndex) Then
MsgBox ("IT'S NOT THAT COLOR! ADD IT!")
' If flag = 0 Then
' Set MyRange = Range(RangeOfCells.Cells(N, 1))
' flag = 1
' MsgBox ("MyRange EXISTS NOW!")
' Else
Set MyRange = Union(MyRange, Range(RangeOfCells.Cells(N, 1)))
MsgBox ("STILL ALIVE AFTER UNION?") 'debug
' End If
End If
MsgBox ("MADE IT OUT!") 'debug
Next N
MsgBox ("Range Output: " & SubRange.Address(ReferenceStyle:=xlA1)) 'debug
ExcludeColoredCellRange = SubRange.Address(ReferenceStyle:=xlA1)
End Function