Add error code if named range is not present

I would like to add an error exception to the code below.
If the table range is not in the sheet, it results in an error.
I would like it to just keep running if the table range is not present in the Sheet.

Thanks!


Sub createrangenames()
    createtableranges "Domestic First Overnight", "FO", 1
    createtableranges "Domestic Priority Overnight", "PO", 1
    createtableranges "Domestic Standard Overnight", "SO", 1
    createtableranges "Domestic 2 Day AM", "E2AM", 1
    createtableranges "Domestic 2 Day", "E2", 1
    createtableranges "Domestic Express Saver", "ESP", 1
    createtableranges "Domestic 1Day Freight", "F1", 2
    createtableranges "Domestic 2Day Freight", "F2", 2
    createtableranges "Domestic 3Day Freight", "F3", 2
       

End Sub

Sub createtableranges(title, codename, tbltype)
    Dim tr As Range
    Set tr = ActiveSheet.Cells.Find(title, ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell))
   
  
   If tbltype = 1 Then
    ActiveSheet.Names.Add codename & "L_2", Me.Range(tr.Offset(3), tr.Offset(3).End(xlToRight))
    ActiveSheet.Names.Add codename & "P_2", ActiveSheet.Range(tr.Offset(4), tr.Offset(4).End(xlToRight).Offset(1))
    ActiveSheet.Names.Add codename & "_2", ActiveSheet.Range(tr.Offset(6), tr.Offset(6).End(xlToRight).End(xlDown))
    ActiveSheet.Names.Add codename & "HW_2", ActiveSheet.Range(tr.Offset(160), tr.Offset(160).End(xlToRight).End(xlDown))
   End If
   
    If tbltype = 2 Then
        ActiveSheet.Names.Add codename & "_2", ActiveSheet.Range(tr.Offset(7), tr.Offset(7).End(xlToRight).End(xlDown))
    End If
End Sub

Open in new window

Euro5Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saqib Husain, SyedEngineerCommented:
Sub createrangenames()
    createtableranges "Domestic First Overnight", "FO", 1
    createtableranges "Domestic Priority Overnight", "PO", 1
    createtableranges "Domestic Standard Overnight", "SO", 1
    createtableranges "Domestic 2 Day AM", "E2AM", 1
    createtableranges "Domestic 2 Day", "E2", 1
    createtableranges "Domestic Express Saver", "ESP", 1
    createtableranges "Domestic 1Day Freight", "F1", 2
    createtableranges "Domestic 2Day Freight", "F2", 2
    createtableranges "Domestic 3Day Freight", "F3", 2
       

End Sub

Sub createtableranges(title, codename, tbltype)
    Dim tr As Range
    Set tr = ActiveSheet.Cells.Find(title, ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell))
if tr is nothing then 
    msgbox "Table " & title & " Not found"
    exit sub
end if    
on error resume next  
   If tbltype = 1 Then
    ActiveSheet.Names.Add codename & "L_2", Me.Range(tr.Offset(3), tr.Offset(3).End(xlToRight))
    ActiveSheet.Names.Add codename & "P_2", ActiveSheet.Range(tr.Offset(4), tr.Offset(4).End(xlToRight).Offset(1))
    ActiveSheet.Names.Add codename & "_2", ActiveSheet.Range(tr.Offset(6), tr.Offset(6).End(xlToRight).End(xlDown))
    ActiveSheet.Names.Add codename & "HW_2", ActiveSheet.Range(tr.Offset(160), tr.Offset(160).End(xlToRight).End(xlDown))
   End If
   
    If tbltype = 2 Then
        ActiveSheet.Names.Add codename & "_2", ActiveSheet.Range(tr.Offset(7), tr.Offset(7).End(xlToRight).End(xlDown))
    End If

on error goto 0
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.