Solved

Create Excel Dynamic Named Range using VBA

Posted on 2014-09-15
6
414 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
  • 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 25

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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 25

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 25

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel cell formatting 5 27
Excel Pivot Chart - Need to show sum of totals 3 17
Excel format formula for currency 15 21
Excel Cell Total 3 19
Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

816 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now