Consulting

Results 1 to 5 of 5

Thread: Rename my range name

  1. #1

    Question Rename my range name

    In my workbook Sheet name is "INDEX1"

    Range("A1") name is "MyRange_1"

    I want to rename "MyRange_1" to "MyRange_2" through vba

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,456
    Location
    Quote Originally Posted by ilyaskazi
    In my workbook Sheet name is "INDEX1"

    Range("A1") name is "MyRange_1"

    I want to rename "MyRange_1" to "MyRange_2" through vba
    there is no rename capability, you need to delete the name and create a new one.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3

    Question

    Help me with this...


    Sub ChangeNames()
     Dim NameX As Name
     Dim MyRange As New Collection
     Dim myString
    For Each NameX In Names
       If Len(ActiveWorkbook.Names(NameX.Name).Name) > 8 Then
         If Left(ActiveWorkbook.Names(NameX.Name).Name, Len(ActiveWorkbook.Names(NameX.Name).Name) - 6) = "SPM" Then
           MyRange.Add (ActiveWorkbook.Names(NameX.Name).Name)  '# See comment below
         End If
       End If
     Next NameX
    For i = 1 To MyRange.count
    myString = myString & MyRange.Item(i) & Chr(10)
     Next i
    MsgBox myString
     Set MyRange = Nothing
    End Sub
    #= Can i store here cell address of found range name???

  4. #4
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Hello,
    The following should work fine for you:

    Function RenameNamedRange(ByVal OldName As String, ByVal NewName As String) As Boolean
     Dim tStr As String
     tStr = ActiveWorkbook.Names(OldName).RefersTo
     ActiveWorkbook.Names(OldName).Delete
     ActiveWorkbook.Names.Add NewName, tStr
    End Function
    'call using ->RenameNamedRange "rng1", "rng2"
    Matt

  5. #5
    Great. You are incredibly smart!!
    thankyou

Posting Permissions

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