Consulting

Results 1 to 5 of 5

Thread: working with named ranges & automated labelling

  1. #1

    working with named ranges & automated labelling

    Anyone know how to get the name of each named range in an excel worksheet and use those names to label new worksheets in the same workbook? I have been working on some code for automating multiple chart creation and would like to now label each new chart worksheet with the name of the named range which is the source data for the chart....
    Please see code below:

    Option Explicit
    
    Public Sub multichrt()
        Dim i As Integer
        Dim x As Range
        Dim c As Chart
        Dim y As Names
        Dim s As Sheets
        Dim n As Object
        Dim xlName As Excel.Name
        Dim b As Name
        Dim l As Collection
        Dim sh As Variant
    Set y = ActiveWorkbook.Names
    'For Each xlName In y
        'docWord.Bookmarks(xlName.Name).Range.Text
            'Set b = Range(xlName.Value)
            'l.Add (b)
    'Next xlName
    For i = 1 To y.Count
            'If (i = Odd) Then
            'End
            Set x = Range(y(i))
            'Application.Goto Reference:="x"
            'Set c = ThisWorkbook.Charts.Add
            Set c = Charts.Add
            c.ChartType = xlLine
            'ActiveChart.SetSourceData Source:=Sheets("heightdata").Range("C2:C303"),
    c.SetSourceData x, _
            PlotBy:=xlColumns
            'ActiveChart.Location Where:=xlLocationAsObject, Name:="heightdata"
            With ActiveChart
                .HasTitle = True
                '.ChartTitle.Characters.Text = "Building Height Analysis"
                .ChartTitle.Characters.Text = y(i)
                .Axes(xlCategory, xlPrimary).HasTitle = True
                .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = _
                "Number of records"
                .Axes(xlValue, xlPrimary).HasTitle = True
                .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Building Height (m)"
            End With
            'Set s = Application.Sheets
            'Set sh = s(GetLastSheet)
            'sh.Name = "Cstr (y(i))"
            Set n = y(i).Value
            'Set l = n.Name
            c.Name = CStr(n)
        Next i
    End Sub

  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi Erica, and welcome to VBAX!

    Here's two options for you, depening on which route you want to go. Please note that there is no error handling in this, so if the sheet name already exists, it will bomb on you. We can fix that though as well.

    Sub Ver1()
    'Creates a new worksheet for each named range
    'in the workbook
    Dim xlName As Name, ws As Worksheet
    For Each xlName In Parent.Names
        Set ws = Worksheets.Add
        ws.Name = xlName.Name
    Next xlName
    End Sub
    
    Sub Ver2()
    'Creates a new worksheet for each named range
    'referring to the active worksheet only
    Dim xlName As Name, ws As Worksheet, shtName As String
    shtName = ActiveSheet.Name
    For Each xlName In Parent.Names
        If Mid(xlName.RefersToR1C1, 2, Len(shtName)) = shtName Then
            Set ws = Worksheets.Add
            ws.Name = xlName.Name
        End If
    Next xlName
    End Sub
    Let me know if you have any questions,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    For Each xlName In Parent.Names 
    Set ws = Worksheets.Add 
    ws.Name = xlName.Name 
    Next xlName


    can be simplified to


    For Each xlName In Parent.Names 
    Worksheets.Add.Name = xlName.Name 
    Next xlName

    saves a statement and a Set.

  4. #4
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hey cool!

    Thanks, xld, and Welcome to VBAX!
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  5. #5
    Thanks all, problem solved! Very helpful indeed.....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •