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?
 
Saqib Husain, SyedEngineerCommented:
There is no table called

Domestic Freight 1Day
0
 
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
Saqib Husain, SyedEngineerCommented:
Please post your file
0
 
Euro5Author Commented:
0
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.