Solved

Create Excel Dynamic Named Range using VBA

Posted on 2014-09-15
6
405 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
My experience with Windows 10 over a one year period and suggestions for smooth operation
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

747 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

14 Experts available now in Live!

Get 1:1 Help Now