+ Reply to Thread
Results 1 to 9 of 9

Macro to delete names except Print_Area

  1. #1

    Macro to delete names except Print_Area

    I have a macro that deletes all of the named ranges in my workbook. The
    problem is that it also deletes the "Print_Area" range, which results
    in resetting my print area.

    Is there a way to modify this so that it will keep my old print area?

    Sub DeleteNames()
    '
    ' Gets rid of all named ranges
    '
    For Each nName In Names
    nName.Delete
    Next
    '
    End Sub


  2. #2
    John
    Guest

    Re: Macro to delete names except Print_Area

    Dear Barber,

    Have a go with the code below. The Print_Range name gets prefixed with the
    sheet it applies to so you need to use the 'Right' function to check the
    last part of the returned string. So what it's saying is if the last 10
    characters of each name do not end in "Print_Area" then delete them. (You
    can delete the Debug.Print... line which just shows you what's going on in
    the Intermediate window of the VBE.)

    Best regards

    John

    Sub DeleteNames()
    ' Gets rid of all named ranges
    Dim nName As Name

    For Each nName In Names
    Debug.Print nName.Name
    If Right(nName.Name, 10) <> "Print_Area" Then
    nName.Delete
    End If
    Next nName

    End Sub

    <[email protected]> wrote in message
    news:[email protected]...
    >I have a macro that deletes all of the named ranges in my workbook. The
    > problem is that it also deletes the "Print_Area" range, which results
    > in resetting my print area.
    >
    > Is there a way to modify this so that it will keep my old print area?
    >
    > Sub DeleteNames()
    > '
    > ' Gets rid of all named ranges
    > '
    > For Each nName In Names
    > nName.Delete
    > Next
    > '
    > End Sub
    >




  3. #3
    John
    Guest

    Re: Macro to delete names except Print_Area

    Dear Barber,

    Have a go with the code below. The Print_Range name gets prefixed with the
    sheet it applies to so you need to use the 'Right' function to check the
    last part of the returned string. So what it's saying is if the last 10
    characters of each name do not end in "Print_Area" then delete them. (You
    can delete the Debug.Print... line which just shows you what's going on in
    the Intermediate window of the VBE.)

    Best regards

    John

    Sub DeleteNames()
    ' Gets rid of all named ranges
    Dim nName As Name

    For Each nName In Names
    Debug.Print nName.Name
    If Right(nName.Name, 10) <> "Print_Area" Then
    nName.Delete
    End If
    Next nName

    End Sub

    <[email protected]> wrote in message
    news:[email protected]...
    >I have a macro that deletes all of the named ranges in my workbook. The
    > problem is that it also deletes the "Print_Area" range, which results
    > in resetting my print area.
    >
    > Is there a way to modify this so that it will keep my old print area?
    >
    > Sub DeleteNames()
    > '
    > ' Gets rid of all named ranges
    > '
    > For Each nName In Names
    > nName.Delete
    > Next
    > '
    > End Sub
    >




  4. #4
    Ardus Petus
    Guest

    Re: Macro to delete names except Print_Area

    Sub DeleteNames()
    '
    ' Gets rid of all named ranges except Print_Area
    '
    For Each nName In Names
    if nName.Name <> "Print_area" then
    nName.Delete
    end if
    Next
    '
    End Sub

    HTH
    '--
    AP

    <[email protected]> a écrit dans le message de
    news:[email protected]...
    > I have a macro that deletes all of the named ranges in my workbook. The
    > problem is that it also deletes the "Print_Area" range, which results
    > in resetting my print area.
    >
    > Is there a way to modify this so that it will keep my old print area?
    >
    > Sub DeleteNames()
    > '
    > ' Gets rid of all named ranges
    > '
    > For Each nName In Names
    > nName.Delete
    > Next
    > '
    > End Sub
    >




  5. #5
    Tom Ogilvy
    Guest

    RE: Macro to delete names except Print_Area

    Sub DeleteNames()
    '
    ' Gets rid of all named ranges
    '
    For Each nName In Names
    if instr(1,nName,Name,"Print_Area",vbtextcompare) = 0 then
    nName.Delete
    end if
    Next
    '
    End Sub

    --
    Regards,
    Tom Ogilvy



    "[email protected]" wrote:

    > I have a macro that deletes all of the named ranges in my workbook. The
    > problem is that it also deletes the "Print_Area" range, which results
    > in resetting my print area.
    >
    > Is there a way to modify this so that it will keep my old print area?
    >
    > Sub DeleteNames()
    > '
    > ' Gets rid of all named ranges
    > '
    > For Each nName In Names
    > nName.Delete
    > Next
    > '
    > End Sub
    >
    >


  6. #6
    Dave Peterson
    Guest

    Re: Macro to delete names except Print_Area

    Typo alert patrol:

    If InStr(1, nName.Name, "Print_Area", vbTextCompare) = 0 Then

    (dot instead of a comma in nName.Name)

    Tom Ogilvy wrote:
    >
    > Sub DeleteNames()
    > '
    > ' Gets rid of all named ranges
    > '
    > For Each nName In Names
    > if instr(1,nName,Name,"Print_Area",vbtextcompare) = 0 then
    > nName.Delete
    > end if
    > Next
    > '
    > End Sub
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    > "[email protected]" wrote:
    >
    > > I have a macro that deletes all of the named ranges in my workbook. The
    > > problem is that it also deletes the "Print_Area" range, which results
    > > in resetting my print area.
    > >
    > > Is there a way to modify this so that it will keep my old print area?
    > >
    > > Sub DeleteNames()
    > > '
    > > ' Gets rid of all named ranges
    > > '
    > > For Each nName In Names
    > > nName.Delete
    > > Next
    > > '
    > > End Sub
    > >
    > >


    --

    Dave Peterson

  7. #7
    Dave Peterson
    Guest

    Re: Macro to delete names except Print_Area

    Be careful.

    Excel creates names that it uses (without your knowledge and without your
    permission).

    If you delete those names, you could be breaking something that excel needs.

    If I were you, I'd get a copy of Jan Karel Pieterse's (with Charles Williams and
    Matthew Henson) Name Manager:

    You can find it at:
    NameManager.Zip from http://www.oaltd.co.uk/mvp

    You can delete just the names you want pretty easily.

    [email protected] wrote:
    >
    > I have a macro that deletes all of the named ranges in my workbook. The
    > problem is that it also deletes the "Print_Area" range, which results
    > in resetting my print area.
    >
    > Is there a way to modify this so that it will keep my old print area?
    >
    > Sub DeleteNames()
    > '
    > ' Gets rid of all named ranges
    > '
    > For Each nName In Names
    > nName.Delete
    > Next
    > '
    > End Sub


    --

    Dave Peterson

  8. #8

    Re: Macro to delete names except Print_Area

    Thanks for your input guys. Both John's and Tom's macros worked great
    for what i need.

    And thanks Dave for the heads up on the excel-created named ranges and
    the Name Manager add-in. While the Name Manager wasn't what i was
    needing for this task, it is a valuable add-in. I was able to clear up
    some redundant local names as well as get rid of some unused named
    ranges. And i'm sure i'll find a use for it in the future as well.

    Thanks again for all of your help. You guys are amazing.

    Ben


  9. #9
    Registered User
    Join Date
    06-02-2013
    Location
    Colorado Springs CO
    MS-Off Ver
    Excel 2013
    Posts
    59

    Re: Macro to delete names except Print_Area

    How can I add another range name not to be deleted?
    I don't want to delete Print_Areas or Print_Titles.

    Thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1