Add a new named range each time a cell changes

Leonvl

New Member
Joined
Apr 26, 2016
Messages
20
When a cell in the range A1:A100 is changed (added), I want to define a named range that referring to the two cells to the right. The name of the range can be the value of the target cell. I tried the below code but it doesnt work for some reason...

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim NamedRange As Range
Dim RangeName As String


If Not Intersect(Target, Range("A1:A100")) Is Nothing Then
    Set NamedRange = Worksheets("Blad1").Range(Target.Offset(, 1), Target.Offset(, 2))
    RangeName = Target.Value
    Target.Names.Add Name:=RangeName, RefersTo:=NamedRange
End If
End Sub
 
I would have done it this way.
But if you want to stick with your way that's fine with me.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified 6/17/18 3:10 AM EDT
If Not Intersect(Target, Range("A1:A100")) Is Nothing Then
If Target.Cells.CountLarge > 1 Or IsEmpty(Target) Then Exit Sub
ans = Replace(Target.Value, " ", "")
Target.Offset(, 1).Resize(, 2).Name = ans
End If
End Sub
 
Upvote 0

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

Forum statistics

Threads
1,224,836
Messages
6,181,250
Members
453,026
Latest member
cknader

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