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
Solved

Create Excel Dynamic Named Range using VBA

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Office Picture Manager is not included in Office 2013. This comes as a shock to users upgrading from earlier versions of Office, such as 2007 and 2010, where Picture Manager was included as a standard application. This article explains how…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
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…

829 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