Solved

Create Excel Dynamic Named Range using VBA

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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Today companies are subjected to more-and-more data, and it won't stop any time soon.  But there are obvious opportunities for reducing data, particularly data duplicated among companies.
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
An overview on how to enroll an hourly employee into the employee database and how to give them access into the clock in terminal.
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…

773 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