To the OP
To avoid 1004 errors, you might add code check that the two ranges are on
the same worksheet before try to intersect them.
For example
Intersect(worksheets(1).Rows(1),worksheets(2).columns(1)).Address
raises a 1004 error.
Another approach is to error trap. Here is some code I posted back in
October 1999 (slightly modified) that demonstrates that approach:
Sub TestName()
Dim rng As Range
definedName as Name
On Error Resume Next
For Each definedName In ThisWorkbook.Names
Set rng = Nothing
Set rng = definedName.RefersToRange
If Not rng Is Nothing Then
If Not Intersect(rng, ActiveCell) Is Nothing Then
MsgBox ActiveCell.Address & " is in a range named: " & definedName.Name
End If
End If
Next
End Sub
This has the advantage that it accounts for defined names that are not
ranges as well. (a defined name does not have to refer to a range).
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.