Macro to create ranges

I need a macro to create ranges on my sheets.
My sample has the ranges selected - I need some code to create these.
The ranges will be in rows, the only reference point is the header names.

The header names are the services:
Domestic Priority Overnight            
Domestic Standard Overnight            
            
The relation between the header names and the ranges will always be the same.
For instance, the first range, POL_1 for Domestic Priority Overnight will always be 3 rows down.
The range will always be from column A to farthest right filled column.

second range, POP_1 will always be from Domestic Priority Overnight, 4 rows down, including 2 rows.
The range will always be from the column A to farthest right filled column.

third range, PO_1 will always be from Domestic Priority Overnight, 5 rows down.
The range will always be from the column A to farthest right filled column.

THANKS!!
SAMPLE.xlsx
Euro5Asked:
Who is Participating?
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.

SteveCommented:
Have you considered using OFFSET and INDEX/MATCH/COUNT to set up dynamic ranges without the need for code?
Or must it be using VBA Macro.

Either is acheivable, so would be about your preference.
0
Saqib Husain, SyedEngineerCommented:
Try this macro. You can add more lines to the first macro

Sub createrangenames()
    createtableranges "Domestic Priority Overnight", "PO"
    createtableranges "Domestic Standard Overnight", "SO"
End Sub

Sub createtableranges(title, codename)
    Dim tr As Range
    Set tr = ActiveSheet.Cells.Find("Domestic ", ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell))
   
    ActiveSheet.Names.Add codename & "L_1", Me.Range(tr.Offset(4), tr.Offset(4).End(xlToRight))
    ActiveSheet.Names.Add codename & "P_1", ActiveSheet.Range(tr.Offset(5), tr.Offset(5).End(xlToRight).Offset(1))
    ActiveSheet.Names.Add codename & "_1", ActiveSheet.Range(tr.Offset(7), tr.Offset(7).End(xlToRight).End(xlDown))
End Sub
0
Euro5Author Commented:
Steve, I don't know that it has to be a macro, but I'm not sure how that Offset would work?
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Euro5Author Commented:
Syed,
The macro chooses the first "Domestic   " on the sheet and begins to create the range names.
However, there are other instance of "Domestic " before the headers, so it is creating the ranges in the wrong places.

Is there are way to search for each specific header and then create the range?
0
Saqib Husain, SyedEngineerCommented:
Sorry it should have been
Sub createrangenames()
    createtableranges "Domestic Priority Overnight", "PO"
    createtableranges "Domestic Standard Overnight", "SO"
End Sub

Sub createtableranges(title, codename)
    Dim tr As Range
    Set tr = ActiveSheet.Cells.Find(title, ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell))
   
    ActiveSheet.Names.Add codename & "L_1", Me.Range(tr.Offset(4), tr.Offset(4).End(xlToRight))
    ActiveSheet.Names.Add codename & "P_1", ActiveSheet.Range(tr.Offset(5), tr.Offset(5).End(xlToRight).Offset(1))
    ActiveSheet.Names.Add codename & "_1", ActiveSheet.Range(tr.Offset(7), tr.Offset(7).End(xlToRight).End(xlDown))
End Sub 

Open in new window

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:
PERFECT THANK YOU!!!!!
0
Euro5Author Commented:
Excellent
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.

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.