New name range created showing range as '"Sheet2!$A$1" and not Sheet2!$A$1.

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
hi, myname range created is showing range as '"Sheet2!$A$1" instead of Sheet2!$A$1 when i hit ctrl + f3 and checked it...
I need to name a range for all the words in col a if not blank.

I got this far but names are created with error.

Please advice on how to correct this.
My current code as below.
Code:
Sub try()
Dim i As Long
Dim sh As Worksheet
For i = 1 To 200
If Range("A" & i).Value <> "" Then
   Range("A" & i).Select
   ActiveWorkbook.Names.Add Name:=ActiveCell.Value, RefersToR1C1:=ActiveSheet.Name & "!" & ActiveCell.Address
    ActiveWorkbook.Names(ActiveCell.Value).Comment = ""
End If
Next i
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
I think this is correct. Looks like it is working:)
Code:
Dim sht As Worksheet
Dim r As Range
For i = 1 To 10
If Range("A" & i).Value <> "" Then
Range("A" & i).Select
Set r = ActiveCell(1, 1)
ActiveWorkbook.Names.Add Name:=ActiveCell.Value, RefersTo:=r
End If
Next i
 
Upvote 0
Hi pedie

You don't usually select objects in vba.

Try, for ex.:

Code:
Sub Test()
Dim r As Range
 
For Each r In Range("A1:A10")
    If r.Value <> "" Then ActiveWorkbook.Names.Add Name:=r.Value, RefersTo:=r
Next r
End Sub
 
Upvote 0
PGC, cool thanks for suggestion. Your code is more simplier and sure works better as you know better...:biggrin:. "m new learner...!
Months back i guess Norie helped me with something like but cant locate it anymore...so


Thanks again!

One more thing please...
How do i delete all the created name range in active sheet?
Too many name range..i wanna delete all of them.
 
Upvote 0
How do i delete all the created name range in active sheet?

Hi

Try this code to delete all the names that refer to a range in the active sheet:

Code:
Sub Test()
Dim nam As Name
Dim r As Range
 
On Error Resume Next
For Each nam In ActiveWorkbook.Names
    Set r = Nothing
    Set r = nam.RefersToRange
    If Not r Is Nothing Then _
        If r.Parent Is ActiveSheet Then _
            nam.Delete
Next nam
End Sub
 
Upvote 0
sorry PGC, i think we cross posted. thanks again for the code.

You have a great day!
Bye for now.

Code:
 Dim nm As Name
    For Each nm In ActiveWorkbook.Names
        nm.Delete
    Next nm
 
Last edited:
Upvote 0
Hi again

Notice that this last code you posted does not answer your request:

How do i delete all the created name range in active sheet?

The code you posted in post#6 will delete all the names in the workbook, including:

- names that do not refer to ranges
- names that refer to ranges not in the active sheet

which is not what you wanted.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,802
Messages
6,126,986
Members
449,351
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