Union of named ranges not working

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
14,076
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
I have three named ranges as seen in the macro below. The macro generates the error: Run-time error '1004': Method' Range' of object' _Worksheet' failed with the line in yellow highlighted on Debug. Would anyone have any suggestions as to the cause of the error?
Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Dim fnd As Range, rng As Range
    Set rng = Union(Range("MondayE"), Range("MondayG"), Range("MondayI"))
    If Not Intersect(Target, rng) Is Nothing Then
        Set fnd = Sheets("Lists").Range("A:A").Find(Target.Value, LookIn:=xlValues, lookat:=xlWhole)
        If Not fnd Is Nothing Then
            fnd.Resize(2).Copy Range(Target.Address)
        End If
    End If
    Application.EnableEvents = True
    Application.ScreenUpdating = False
End Sub
 
Your code is in a worksheet_Change event so activating the other sheet will make no difference. You have to qualify any range that is not on the worksheet containing the code with its parent sheet.
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
I have to apologize to all those members who have responded. I had a long day yesterday and I wasn't thinking properly. The named ranges are not on a different sheet. They are on the same sheet as the worksheet_Change event. I get the same error even if I don't use Union as in the code below:
VBA Code:
Set rng = Range("MondayE")
 
Upvote 0
Hopefully Rory will have some other insights, since it works fine for me.
Change event is in Sheet2

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rngComb As Range
    Set rngComb = Union(Range("rngA"), Range("rngD"), Range("rngG"))
    
    Debug.Print rngComb.Address(external:=True)
End Sub

1702216053453.png


1702216008082.png
 
Upvote 0
I believe that I have solved my problem. Upon further examination, I found that one of the three named ranges was not highlighting the cells in the named range which indicated an error in the range. I recreated that named range and it is now working properly. Thanks to all for your suggestions and time spent.
 
Upvote 0

Forum statistics

Threads
1,223,855
Messages
6,175,023
Members
452,603
Latest member
bendarasdavide

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top