Consulting

Results 1 to 8 of 8

Thread: Solved: Insert New Range Name

  1. #1

    Solved: Insert New Range Name

    I'm trying to cycle through a handful of ranges to insert new range names based on the name of the current tab.

    The string variables work. The re-positioning works. But my code inserts the first range name in the very last position. It does not insert any other name.

    Can anybody tell me what I'm doing wrong?



    [vba]Dim ThisTab As String
    ThisTab = Range("AK1").Value

    Dim n As Integer
    n = 1 'cycles through to 5

    Dim NameMe As String
    Dim Refers As String


    NameMe = ThisTab & n & "_"
    Refers = ThisTab & "!R3C29"
    Application.Goto Reference:=Refers
    ActiveWorkbook.Names.Add Name:=NameMe, RefersToR1C1:=ActiveCell
    'these don't work either:
    'ActiveWorkbook.Names.Add Name:=NameMe, RefersToR1C1:= "=" & ActiveCell
    'ActiveWorkbook.Names.Add Name:=NameMe, RefersToR1C1:= "=" & Refers

    ActiveWorkbook.Names(NameMe).Comment = ""
    [/vba]

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    untested: after the Goto line:
    Activecell.name = NameMe
    you probably don't need the Refers variable.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    Sadly, ActiveCell.Name = NameMe doesn't work.

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Quote Originally Posted by skulakowski
    Sadly, ActiveCell.Name = NameMe doesn't work.
    'Doesn't work' doesn't give me much to go on.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    I wish I could be more specific and identify an error but there is none. The code runs completely but doesn't add any range name.

    The problem appears to be with using NameMe as a variable, not a string enclosed in quotes, in Names.Add function
    [vba]ActiveWorkbook.Names.Add Name:=NameMe[/vba]
    If I hardcode any string, i.e., "NameHere" with quotes, the code inserts the range name. So I think I need to add a quote, before and after, as part of the NameMe string.

    To incorporate the beginning/ending quote, I tried redefining NameMe as
    [vba]NameMe = Chr(34) & ThisTab & n & "_" & Chr(34)
    [/vba]
    but this gives me a run-time 1004 error, "the name you entered is not valid". Among the suggested reasons are "name begins with an invalid character".

  6. #6
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    I think that this might work for you
    [VBA]ActiveWorkbook.Names.Add Name:=NameMe, RefersTo:= "=" & ActiveCell.Address(,,,True) [/VBA]

    However the whole thing could be replaced with
    [VBA]Dim ThisTab As String
    ThisTab = Range("AK1").Value

    Dim n As Integer
    n = 1 'cycles through to 5

    Dim NameMe As String

    NameMe = ThisTab & n & "_"

    ThisWorkbook.Sheets("ThisTab").Cells(3, 29) = NameMe

    ActiveWorkbook.Names(NameMe).Comment = "" [/VBA]

    The idea of n cycling from 1 to 5 is a bit confusing. Do you want ThisTab!AC3 to have 5 names?

    RE:"The code runs completely".
    That's odd, Comment is not a method of a Name object, I would expect that line to error. It did in my testing.
    If this code is inside an On Error Resume Next section, removing that might help track down the error.

  7. #7
    Based on this Microsoft article, How to programmatically copy all range names in Excel (http://support.microsoft.com/kb/213389), I've discovered that writing the NameMe string to a cell + resetting the value of NameMe as the cell contents works.

    [vba]Refers = ThisTab & "!R3C29"
    NameMe = ThisTab & n & "_"
    Range("a1").Value = NameMe
    NameMe = Range("a1").Value

    ActiveWorkbook.Names.Add Name:=NameMe, RefersToR1C1:="=" & Refers
    [/vba]
    Seems inelegant to me but this 'trick' of writing out and resetting NameMe does indeed insert new range names.

  8. #8

    Thumbs up

    final code
    [vba]Dim ThisTab As String
    ThisTab = Range("AK1").Value

    Dim NameMe As String
    Dim Refers As String

    Dim n As Integer
    Dim c As Integer
    Dim r As Integer
    r = 3

    For n = 1 To 5

    c = 29
    NameMe = ThisTab & n & "_"
    Refers = ThisTab & "!R" & r & "C" & c
    Range("a1").Value = NameMe
    NameMe = Range("a1").Value
    ActiveWorkbook.Names.Add Name:=NameMe, RefersToR1C1:="=" & Refers

    c = 30
    NameMe = ThisTab & n & "1_"
    Refers = ThisTab & "!R" & r & "C" & c
    Range("a1").Value = NameMe
    NameMe = Range("a1").Value
    ActiveWorkbook.Names.Add Name:=NameMe, RefersToR1C1:="=" & Refers

    r = r + 10

    Next n

    Range("a1).Value = ""
    [/vba]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •