Consulting

Results 1 to 6 of 6

Thread: Delete Hidden Name Ranges

  1. #1
    VBAX Newbie
    Joined
    Oct 2007
    Posts
    2
    Location

    Delete Hidden Name Ranges

    Hi guys,
    How do I delete hidden name ranges? The following code below unhides the hidden name ranges, but it won't delete them! I have to manually delete them one by one, and it's annoying. If you run the macro on the attached file, you get an error message. Please help me!

    Sub Remove_Hidden_Names()
           ' Dimension variables.
           Dim xName As Variant
           ' Loop once for each name in the workbook.
    For Each xName In ActiveWorkbook.Names
               If xName.Visible = True Then
                   xName.Delete
                Else
                    xName.Visible = True
                    xName.Delete
                End If
    Next xName
    End Sub

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    The first named range in your list is invalid in some way. If you delete it manually then your code runs ok.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Newbie
    Joined
    Oct 2007
    Posts
    2
    Location
    Lucas,
    Do you know what's invalid about the name? I know it's a really long name, do you think that has something to do with it?

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    I honestly don't know. It seems like just a letter and numbers with no spaces.....should b ok but there is something wrong there for sure.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    My guess is that D01203 (or DO1203) looks like a cell address to Excel and it won't let you delete it from VBA, but you can from the worksheet side (go figure).

    However, just to make sure that you stay confused, you cannot add that as a name from the worksheet side, but you can from the VBA side , but only if you use :=RefersToR1C1 but not :=RefersTo

    [vba]
    Sub Remove_Hidden_Names()

    Dim i As Long
    For i = ActiveWorkbook.Names.Count To 1 Step -1
    With ActiveWorkbook.Names(i)
    .Visible = True
    .Delete
    End With
    Next i
    End Sub

    Sub Add_Hidden_Names()

    'works
    ' ActiveWorkbook.Names.Add Name:="D01203", RefersToR1C1:="=R1C1", Visible:=False

    ' does not work
    ActiveWorkbook.Names.Add Name:="D01203", RefersTo:="=A1", Visible:=False

    End Sub


    [/vba]

  6. #6
    Ah ! ... more consistent coding from the guys at Microsoft ...
    2+2=9 ... (My Arithmetic Is Mental)

Posting Permissions

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