Solved

Create Excel Dynamic Named Range using VBA

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

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 …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

743 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

15 Experts available now in Live!

Get 1:1 Help Now