Solved

Create Excel Dynamic Named Range using VBA

Posted on 2014-09-15
6
418 Views
Last Modified: 2014-09-15
is there a way to create excel dynamic named ranges using VBA that in one go, for all active worksheets in a workbook dynamic named ranges for columns based on the top row header are created?

thanks.
0
Comment
Question by:ProfessorJimJam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40323818
Do you want to create named ranges for individual columns so that the header row label is also the name of the range?  If so, you might want a macro that creates Excel Tables which, as a result, would make them dynamically-sized.  

I presume that every sheet that has a value in row 1 of each column means that a range name is to be created.  Please confirm.

-Glenn
0
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40323865
Here is example code that would achieve what I described above.
Sub Create_RangeNames()
    Dim wbk As Workbook
    Dim sht As Worksheet
    Dim rng, rng2 As Range
    Dim cl As Object
    Dim c As Long
    
    Set wbk = ActiveWorkbook
    For Each sht In wbk.Sheets
        c = sht.Cells.SpecialCells(xlLastCell).Column
        Set rng = sht.Range("A1", sht.Range("A1").Offset(0, c))
        For Each cl In rng
            If cl.Value <> "" Then
                Set rng2 = sht.Range(cl, cl.End(xlDown))
                sht.ListObjects.Add(xlSrcRange, rng2, , xlYes).Name = cl.Value
            End If
        Next cl
    Next sht
End Sub

Open in new window


It actually creates Excel Tables and sets their name to be equal to the header row value.  The attached example workbook demonstrates this.

Regards,
-Glenn
EE-CreateTables.xlsm
0
 
LVL 26

Author Comment

by:ProfessorJimJam
ID: 40323885
thank you Glenn. I appreciate your help.

but what I was looking was the named ranges. not excel table.

here is complete detail of what I need.
http://www.mrexcel.com/forum/excel-questions/805529-urgent-help-needed-creating-dynamic-named-ranges-using-visual-basic-applications.html
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 26

Author Comment

by:ProfessorJimJam
ID: 40323902
if It would not be possible to have this through normal Named Range as I have requested then I would accept your earlier solution.
0
 
LVL 26

Author Closing Comment

by:ProfessorJimJam
ID: 40323932
I will accept the table option, as you have spent time writing it for me. but I am initiating a new threat with the specific to the dynamic named range.  and I will put the category under Microsoft Excel Product as you have recommended.
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40324034
I'll look into your other question and see if I can offer a range-specific solution.

The reason I chose the Excel Table option was because one of the characteristics of Excel Tables is that their size is dynamic:  adding a new row of data automatically expands the range.

Regards,
-Glenn
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
Outlook Free & Paid Tools
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question