Solved

Looping thru named ranges and create charts

Posted on 2011-03-02
4
445 Views
Last Modified: 2012-05-11
Dear Experts:

below code ...
... loops thru all the list objects of the active sheet (data lists formatted as tables)
... and formats the chart

I would like the code to loop through all named ranges instead of list objects (defined tables) and create the charts from them. I got no idea how to rewrite the code.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
For Each t In ActiveSheet.ListObjects
    Set MyChtObj = ActiveSheet.ChartObjects.Add _
        (Left:=100, Width:=400, Top:=75, Height:=200)
    
   With MyChtObj.Chart
       .SetSourceData Source:=Sheets("Sheet2").Range(t)
            .ChartType = xlBarClustered
            .HasLegend = False
             With .Axes(xlCategory)
                 .MajorTickMark = xlNone
                 .TickLabels.Font.Name = "Arial"
                 .TickLabels.Font.Size = 9.5
             End With
            .SeriesCollection(1).ApplyDataLabels
             With .SeriesCollection(1).DataLabels
                  .Position = xlLabelPositionInsideEnd
                  .Font.Color = RGB(255, 255, 255)
                  .Font.Name = "Arial"
                  .Font.Size = 9.5
                End With
             With .Axes(xlValue)
                  .MinimumScale = 0
                  .MaximumScale = 10
                  .Delete
                  .MajorGridlines.Delete
             End With
    
        End With
  
        With MyChtObj.Chart
            .SetElement (msoElementChartTitleAboveChart)
            With .ChartTitle
                 .Text = "=Sheet2!$B$6"
                 .Font.Name = "Arial"
                 .Font.Size = 12
            End With
        End With
        

        With MyChtObj.Chart.Parent 'Change location so they're not on top of each other
            .Top = Range("A7").Top + i * (MyChtObj.Height + 80)
            .Left = Range("E7").Left
            .Name = "Chart" & i + 1
        End With
        i = i + 1
Next

End Sub

Open in new window

0
Comment
Question by:AndreasHermle
  • 2
4 Comments
 
LVL 8

Expert Comment

by:ragnarok89
ID: 35019688
I think what you want it:

Sub Test()
    Dim N As Name
    For Each N In Sheet1.Names
        "do stuff"
    Next N
End Sub
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 35026237
Something like:
   Dim rngNamed As Range, nme As Name

   For Each nme In ThisWorkbook.Names
      On Error Resume Next
      Set rngNamed = nme.RefersToRange
      On Error GoTo 0
      If Not rngNamed Is Nothing Then
         If rngNamed.Worksheet.Name = ActiveSheet.Name Then

            Set MyChtObj = ActiveSheet.ChartObjects.Add _
                           (Left:=100, Width:=400, Top:=75, Height:=200)

            With MyChtObj.Chart
               .SetSourceData Source:=rngNamed
               .ChartType = xlBarClustered
               .HasLegend = False
               With .Axes(xlCategory)
                  .MajorTickMark = xlNone
                  .TickLabels.Font.Name = "Arial"
                  .TickLabels.Font.Size = 9.5
               End With
               .SeriesCollection(1).ApplyDataLabels
               With .SeriesCollection(1).DataLabels
                  .Position = xlLabelPositionInsideEnd
                  .Font.Color = RGB(255, 255, 255)
                  .Font.Name = "Arial"
                  .Font.Size = 9.5
               End With
               With .Axes(xlValue)
                  .MinimumScale = 0
                  .MaximumScale = 10
                  .Delete
                  .MajorGridlines.Delete
               End With

            End With

            With MyChtObj.Chart
               .SetElement (msoElementChartTitleAboveChart)
               With .ChartTitle
                  .Text = "=Sheet2!$B$6"
                  .Font.Name = "Arial"
                  .Font.Size = 12
               End With
            End With


            With MyChtObj.Chart.Parent   'Change location so they're not on top of each other
               .Top = Range("A7").Top + i * (MyChtObj.Height + 80)
               .Left = Range("E7").Left
               .Name = "Chart" & i + 1
            End With
            i = i + 1
         End If
      End If
   Next nme

End Sub

Open in new window

0
 

Author Comment

by:AndreasHermle
ID: 35040828
Dear rorya:

thank you very much for your quick help. I am getting erroneous results but it may be because I am running the code on Excel 2010. This version has a couple of charting bugs.

I will run the code on my machine at my working  place (Excel 2007) and then let you know what happened. Again, thank you very much for your professional support.

Regards, Andreas
0
 

Author Closing Comment

by:AndreasHermle
ID: 35058674
rorya:
Great job rorya!!
ok, now it works just fine. But it has nothing to with Excel 2010 and Excel 2007, respectively, as I initially thought.
Thank you very much for your great and professional help. What would I do without this forum?

Regards, Andreas
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

830 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