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?
Thank you and Kind regards,
Nhinx
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