Solved

Create Excel Dynamic Named Range using VBA

Posted on 2014-09-15
4
2,075 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?

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
Comment
Question by:ProfessorJimJam
  • 2
4 Comments
 
LVL 33

Expert Comment

by:Norie
ID: 40324069
Couldn''t you use something like this?
myName = ws.Name & Replace(Cells(Rowno, i).Value, " ", "_")

Open in new window


By the way, if these are sheet level named ranges shouldn't you be including the worksheet name in the formulas for the dynamic ranges?

PS Have you considered using tables?
0
 
LVL 25

Author Comment

by:ProfessorJimJam
ID: 40324103
When I use dynamic named ranges some of named ranges will be identical that is why I need the sheet name as a prefix to the named range, so that I understand which named range is belonging to which sheet.  

Table option wouldn't work, because again the table wouldn't tell the user the sheet source. One expert named Glenn has provided me with the table option in another thread, but it wouldn't be desired option. So I am waiting for any expert to chip in and help me on this dynamic range dilemma
0
 
LVL 33

Expert Comment

by:Norie
ID: 40324130
Did you try what I suggested?

it will add the sheet name to the start of the dynamic named range names.

As for not being able to determine the source sheet of a table, that should be possible using VBA, perhaps using a UDF.
0
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40324198
I would abandon the assignment of lrow, lcol,and Start altogether and just enter the OFFSET function directly into the range name assignment like so:
Sub Create_RangeNames()
    'This creates range names based on header row information
    Dim wbk As Workbook
    Dim sht As Worksheet
    Dim rng, rng2 As Range
    Dim cl As Object
    Dim c As Long
    Dim strAddr As Variant
    Dim strShName, strHdrName, strCol As String
    
    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
                strShName = Replace(sht.Name, " ", "_", 1)
                strHdrName = Replace(cl.Value, " ", "_", 1)
                strAddr = Split(cl.Address, "$")
                strCol = "$" & strAddr(1) & ":$" & strAddr(1)
                Set rng2 = sht.Range(cl, cl.End(xlDown))
                ActiveWorkbook.Names.Add Name:=strShName & strHdrName, _
                    RefersTo:="=OFFSET('" & sht.Name & "'!" & cl.Address & ",0,0,COUNTA('" & _
                    sht.Name & "'!" & strCol & "),1)"
            End If
        Next cl
    Next sht
End Sub

Open in new window


This replaces any spaces in the sheet name or header row value with an underscore and combines them to create the range name.  I've attached a sample workbook with sample ranges already created, but you can delete them and re-run this macro to test.

-Glenn
EE-CreateDynRanges.xlsm
1

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

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…
This article will shed light on the latest trends when it comes to your resume building needs. For far too long, the traditional CV format has monopolized the recruitment market.
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

919 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