?
Solved

Create Excel Dynamic Named Range using VBA

Posted on 2014-09-15
4
Medium Priority
?
2,454 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 34

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 26

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 34

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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
When asking a question in a forum or creating documentation, screenshots are vital tools that can convey a lot more information and save you and your reader a lot of time
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 …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Suggested Courses

770 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