Adding on VBA to create new range

End Sub
I have the code below so far, but I have other ranges to create that do not follow the same format.
For instance, I have a service "Domestic Freight 1Day", "F1"

The range is     ActiveSheet.Names.Add codename & "_2", ActiveSheet.Range(tr.Offset(4), tr.Offset(4).End(xlToRight).End(xlDown))
This range does not exist in the FO, PO, SO, ect.
How can I add this named range to the existing code?

Thanks!!

Sub createrangenames()
    createtableranges "Domestic First Overnight", "FO"
    createtableranges "Domestic Priority Overnight", "PO"
    createtableranges "Domestic Standard Overnight", "SO"
    createtableranges "Domestic 2 Day AM", "E2AM"
    createtableranges "Domestic 2 Day", "E2"
    createtableranges "Domestic Express Saver", "ESP"
   
       


Sub createtableranges(title, codename)
    Dim tr As Range
    Set tr = ActiveSheet.Cells.Find(title, ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell))
   
    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 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:
You can do something like this. Add a third argument to the createtableranges sub and then check for that argument while assigning the range.

Open in new window


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 Freight 1Day", "F1",2
   
       


Sub createtableranges(title, codename,tbltype)
    Dim tr As Range
    Set tr = ActiveSheet.Cells.Find(title, ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell))
   
    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))
    if tbltype=2 then
        ActiveSheet.Names.Add codename & "HW_2", ActiveSheet.Range(tr.Offset(160), tr.Offset(160).End(xlToRight).End(xlDown))
    end if
End Sub

Open in new window

0
Euro5Author Commented:
Error
Object variable or with block variable not set.
0
Saqib Husain, SyedEngineerCommented:
Please post your file
0
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Euro5Author Commented:
0
Saqib Husain, SyedEngineerCommented:
There is no table called

Domestic Freight 1Day
0

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
Euro5Author Commented:
Sorry! You're right. That eliminated the error.
However, it is still creating too many ranges:
F1HW_2, F1L_1, etc.

Also, is it possible to create an exception where if the named range is not present, it would just move on and not throw an error? I could create a new question for this after we resolve this current question if desired.

Thanks!!!
0
Saqib Husain, SyedEngineerCommented:
I just gave you the concept based on what you provided. You would have to give specifics for anyone to be able to help you further.
0
Saqib Husain, SyedEngineerCommented:
....create an exception where if the named range is not present, it would just move on and not throw an error

Before such a line insert

on error resume next


and after that line

on error go to 0
0
Euro5Author Commented:
Very exceptional help with this.
0
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.