Delete Name Ranges Except Print Area and Print Titles

nhinx

Board Regular
Joined
Aug 25, 2016
Messages
52
Office Version
  1. 2010
Dear Excel Experts,

I'm trying to built in the following code in excel to prevent having a lot of unwanted name ranges in my excel workbook. The code below worked for me but always deleting the Print Titles which kinda frustrating since I always edit files with print titles (Rows to repeat at top) before printing. I tried including the statement If Right(n.Name, 10) <> "Print_Titles" Then n.Delete to the code below but it also delete the Print Area of all of the sheets in the workbook. Can anyone help me solve this issue?

VBA Code:
Private Sub Worksheet_Activate

Dim n As Name
On Error Resume Next
For Each n In ActiveWorkbook.Names
n.visible=true
Debug.Print n.Name

If Right(n.Name, 10) <> "Print_Area" Then n.Delete
If InStr(1, n.RefersTo, "=#NAME?") > 10 Then n.Delete
If InStr(1, n.RefersTo, "=#REF!#REF!") > 10 Then n.Delete
If InStr(1, n.RefersTo, "=#REF!") > 10 Then n.Delete
If InStr(1, n.RefersTo, "=#N/A") > 10 Then n.Delete
If InStr(1, n.RefersTo, "=#N/A,#N/A") > 10 Then n.Delete
If InStr(n.Value, "=#REF!") > 10 Then n.Delete
If InStr(n.Value, "=#REF!#REF!") > 10 Then n.Delete
If InStr(n.Value, "=#N/A") > 10 Then n.Delete

Next n
On Error GoTo 0

End Sub

Thank you and Kind regards,

Nhinx
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Usually the Name "print_area" is of Worksheet Level. ....
VBA Code:
dim nm as Name
for each nm in Me.Names'Me = WorkSheet
    if nm.Name not Like "*Print*Area*" then nm.Delete
next
 
Upvote 0
correction:
if Not nm.Name Like "*Print*Area*" then nm.Delete then
 
Upvote 0
correction:
if Not nm.Name Like "*Print*Area*" then nm.Delete then
Hi Excelogist,

Thanks for the code, your code and my code above are both working. My only problem is the Print Titles not to be deleted during the run. The Print Titles correspond to the rows repeating at top during printing. Say I have a worksheet of 20 pages and during the printing I want row 1 to 6 to be always printed from Page 2 to 20. So that is my dilemma because the Print Area is there but the Print Titles is always removed once the code was executed.


correction:
if Not nm.Name Like "*Print*Area*" then nm.Delete then
 
Upvote 0
Hi Excelogist,

Thanks for the code, your code and my code above are both working. My only problem is the Print Titles not to be deleted during the run. The Print Titles correspond to the rows repeating at top during printing. Say I have a worksheet of 20 pages and during the printing I want row 1 to 6 to be always printed from Page 2 to 20. So that is my dilemma because the Print Area is there but the Print Titles is always removed once the code was executed.
Sorry, my bad. I didn't notice it's "Print 'Title' ". How about :
VBA Code:
If Not (n.Name like "*Print*Area*") or Not (n.Name like "*Print*Title*") Then
    If InStr(1, n.RefersTo, "=#NAME?") > 10 Then n.Delete
    If InStr(1, n.RefersTo, "=#REF!#REF!") > 10 Then n.Delete
    If InStr(1, n.RefersTo, "=#REF!") > 10 Then n.Delete
    If InStr(1, n.RefersTo, "=#N/A") > 10 Then n.Delete
    If InStr(1, n.RefersTo, "=#N/A,#N/A") > 10 Then n.Delete
    If InStr(n.Value, "=#REF!") > 10 Then n.Delete
    If InStr(n.Value, "=#REF!#REF!") > 10 Then n.Delete
    If InStr(n.Value, "=#N/A") > 10 Then n.Delete
end if
 
Upvote 0
@nhinx - not sure that was clear the line should read.
Rich (BB code):
If Not (n.Name like "*Print*Area*") And Not (n.Name like "*Print*Title*") Then

or using your initial methodology:
Rich (BB code):
    If Right(nm.Name, 10) <> "Print_Area" And Right(nm.Name, 12) <> "Print_Titles" Then
 
Upvote 0
Solution
Yes, "And".

@nhinx - not sure that was clear the line should read.
Rich (BB code):
If Not (n.Name like "*Print*Area*") And Not (n.Name like "*Print*Title*") Then

or using your initial methodology:
Rich (BB code):
    If Right(nm.Name, 10) <> "Print_Area" And Right(nm.Name, 12) <> "Print_Titles" Then
Hi Alex,

Both code is working. Though the first one has still more name range with #REF! and #N/A left in the name manager but the second one only few name range with #REF! and #N/A has left and could manage to manually delete it. I've tested it in the excel with a thousands name ranges which make my excel work slow. Thank you very much for your help. It saves my day. You guys rock.
 
Upvote 0

Forum statistics

Threads
1,221,417
Messages
6,159,789
Members
451,589
Latest member
Harold14

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