RangeExclude function
Has anyone written a good, fast function that takes two range arguments,
and returns a range variable equal to the first range excluding the
intersection of the first and second ranges. The function I have
written is very basic and slow.
I would be very interested if there is a faster version of this
function. Thanks in advance.
Public Function RangeExclusion(FromRange As Excel.Range, ExcludeRange As
Excel.Range) As Excel.Range
Dim rngCell As Excel.Range, rngAnswer As Excel.Range
If ExcludeRange Is Nothing Then
Set RangeExclusion = FromRange
Exit Function
ElseIf FromRange Is Nothing Then
Set RangeExclusion = Nothing
Exit Function
End If
For Each rngCell In FromRange
If Application.Intersect(rngCell, ExcludeRange) Is Nothing Then
If rngAnswer Is Nothing Then
Set rngAnswer = rngCell
Else
Set rngAnswer = Union(rngAnswer, rngCell)
End If
End If
Next
Set RangeExclusion = rngAnswer
' Clean up.
Set rngCell = Nothing
Set rngAnswer = Nothing
End Function
|