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
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified 6/11/18 7:10 PM EDT
If Not Intersect(Target, Range("A1:A100")) Is Nothing Then
If Target.Cells.CountLarge > 1 Or IsEmpty(Target) Then Exit Sub
Target.Offset(, 1).Resize(, 2).Name = Target.Value
End If
End Sub
 
Upvote 0
Thanks...! That does the job!

And so a new issue came up... If the target value contains spaces it needs to be trimmed. But "Trim(Target.Value)" seems not acceptable :(

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
If Target.Cells.CountLarge > 1 Or IsEmpty(Target) Then Exit Sub
Target.Offset(, 1).Resize(, 2).Name = Trim(Target.Value)
End If


If Not Intersect(Target, Range("E1:E100")) Is Nothing Then
    Target.Offset(, 1) = ""
End If


End Sub
 
Upvote 0
So what are you doing entering some value like "My Dog is Black"

Give me some example of a value you want trimmed.
 
Upvote 0
Try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified 6/11/18 9:10 PM EDT
If Not Intersect(Target, Range("A1:A100")) Is Nothing Then
If Target.Cells.CountLarge > 1 Or IsEmpty(Target) Then Exit Sub
ans = Trim(Target.Value)
Target.Offset(, 1).Resize(, 2).Name = ans
End If
End Sub
 
Upvote 0
I never click on links or open files.
You need to explain what it does not do.
I asked this question which you did not answer:

So what are you doing entering some value like "My Dog is Black"

Give me some example of a value you want trimmed.
 
Upvote 0
I got it figured!

Code:
Private Sub Worksheet_Change(ByVal Target As Range)


Dim NamedRange As Range
Dim RangeName As String
Dim ans As String


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


If Not Intersect(Target, Range("E1:E100")) Is Nothing Then
    Target.Offset(, 1) = ""
End If


End Sub
 
Upvote 0
Glad to see you have it working. I was just about ready to send you that same answer.
Was not sure how you would want to replace the spaces. With nothing or a underline.
 
Upvote 0

Forum statistics

Threads
1,215,794
Messages
6,126,951
Members
449,350
Latest member
Sylvine

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