updating a named range

xzaqus

New Member
Joined
Sep 24, 2017
Messages
33
Hello,

I have a named range (rangeAdded) that is created with an inputbox
rng = Application.InputBox("Select a range", "Obtain Range", Type:=8)
ThisWorkbook.Names.Add Name:="rangeAdded", RefersTo:=rng

For example, rangeAdded = Sheet1!$D$3:$F$5



Next, I use the inputbox again and select a range. Now, I need this to be added to the existing range so it includes both the old and the new range. If I now select Sheet1!$F$2:$H$5, the final range stored should become

rangeAdded = Sheet1!$D$3:$F$5,Sheet1!$F$2:$H$5

I tried union and simple &, but could not get it to work. Any help will be appreciated.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Code:
Sub NamedRangeModify()
    Dim R1 As Range
    Dim N As Name

    ThisWorkbook.Names.Add "rangeAdded", "=Sheet1!$D$3:$F$5"
    Set N = Names("rangeAdded")
    Debug.Print N.RefersTo
    
    Set R1 = Application.Union(N.RefersToRange, ThisWorkbook.Worksheets("Sheet1").Range("$F$2:$H$5"))
    Debug.Print R1.Address(True, True, xlA1, True)
      
    N.RefersTo = "=" & R1.Address(True, True, xlA1, True)
    Debug.Print N.RefersTo
End Sub
 
Last edited:
Upvote 0
Many thanks.

This was really useful. I ended up modifying it a bit.

As I needed to select the second range through an InputBox, I created a temp Range which I appended to the existing using union and then updated the referTo as well as deleted the temp Range.
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

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