+ Reply to Thread
Results 1 to 4 of 4

Looping through sheets and creating charts issue

Hybrid View

sharadbade Looping through sheets and... 06-18-2014, 11:53 PM
Andy Pope Re: Looping through sheets... 06-19-2014, 03:51 AM
millz Re: Looping through sheets... 06-19-2014, 05:48 AM
sharadbade Re: Looping through sheets... 06-19-2014, 12:04 PM
  1. #1
    Registered User
    Join Date
    06-18-2014
    Location
    usa
    MS-Off Ver
    2010
    Posts
    2

    Question Looping through sheets and creating charts issue

    Hello folks,

    I have following code which is not behaving the way I want.
    Dim current As Worksheet
    Dim chart_name As String
    Dim curr As String
    Dim Columns As ListColumns
    Dim temp As String
    Dim listobj As ListObject
    Dim new_sheet As Worksheet
    
        For Each current In ThisWorkbook.Sheets        
             curr = listobj.Name
            temp = curr & "[[#All],[Column1]]," & curr & "[[#All],[Column2]]"
            current.Shapes.AddChart.Select
            ActiveChart.ChartType = xlColumnStacked
            chart_name = "chart_" & current.Name
            Debug.Print temp
            ActiveChart.SetSourceData Source:=Range(temp)
            ActiveChart.ApplyLayout (1)
            ActiveChart.ChartTitle.Select
            ActiveChart.ChartTitle.Text = "Y: Stacked PKI of instr pairs"
            Selection.Format.TextFrame2.TextRange.Characters.Text = _
                "Y: Stacked PKI of instr pairs"
            With Selection.Format.TextFrame2.TextRange.Characters(1, 29).ParagraphFormat
                .TextDirection = msoTextDirectionLeftToRight
                .Alignment = msoAlignCenter
            End With
            ActiveChart.ChartArea.Select
            ActiveChart.Location Where:=xlLocationAsNewSheet, Name:=chart_name
        Next
    The problem is the last statement, a chart which gets created in an iteration does not "move" to a new sheet but rather same sheet. So end result is a sheet with chart of the last iteration.

    How to get this code to create new charts in new sheet and not the same sheet. A help will be greatly appreciated.
    Last edited by vlady; 06-19-2014 at 01:20 AM.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: Looping through sheets and creating charts issue

    Not really clear from your description what you want.

    Why not try recording the code and then replacing the last biit with correct syntax.

    You probably want to loop through Worksheets rather than Sheets, as sheets includes chartsheets which will not have ranges on it.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Looping through sheets and creating charts issue

    I just tried something and it seems to work fine for me (1 new sheet for each chart) but only after I change ThisWorkbook to ActiveWorkbook.

    This was what I did:
    Dim current As Worksheet
    Dim chart_name As String
    Dim curr As String
    Dim Columns As ListColumns
    Dim temp As String
    Dim listobj As ListObject
    Dim new_sheet As Worksheet
    
        For Each current In ActiveWorkbook.Sheets
            'curr = listobj.Name
            'temp = curr & "[[#All],[Column1]]," & curr & "[[#All],[Column2]]"
            With current
                chart_name = "chart_" & .Name
                With .Shapes.AddChart(xlColumnStacked)
                    .Chart.SetSourceData Source:=current.Range("A1").CurrentRegion 'Range(temp)
                    .Chart.ApplyLayout 1
                    .Chart.ChartTitle.text = "Y: Stacked PKI of instr pairs"
                    .Chart.ChartTitle.Format.TextFrame2.TextRange.Characters.text = _
                        "Y: Stacked PKI of instr pairs"
                    With .Chart.ChartTitle.Format.TextFrame2.TextRange.Characters(1, 29).ParagraphFormat
                        .TextDirection = msoTextDirectionLeftToRight
                        .Alignment = msoAlignCenter
                    End With
                    .Chart.Location Where:=xlLocationAsNewSheet, Name:=chart_name
                End With
            End With
        Next
    多么想要告诉你 我好喜欢你

  4. #4
    Registered User
    Join Date
    06-18-2014
    Location
    usa
    MS-Off Ver
    2010
    Posts
    2

    Re: Looping through sheets and creating charts issue

    Thanks so much Millz. That worked like magic!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Looping code to create charts
    By shumba in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-30-2010, 11:48 PM
  2. Looping through a workbook with sheets and charts
    By Masact in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-30-2009, 05:59 PM
  3. Creating simple charts from multiple pivot table charts
    By hovaucf in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-23-2008, 04:13 PM
  4. creating charts from multiple sheets for powerpoint
    By dbrine in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 05-18-2008, 11:58 AM
  5. Looping Through Charts / ChartObjects
    By William Benson in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-28-2005, 09:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1