Solved

Create Excel Dynamic Named Range using VBA

Posted on 2014-09-15
6
410 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

863 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

20 Experts available now in Live!

Get 1:1 Help Now