+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    10-30-2009
    Location
    Wisconsin, USA
    MS-Off Ver
    Excel 2003
    Posts
    3

    Question Adding charts to multiple worksheets automatically

    I am trying to automatically add charts (4) to mulitple worksheets (39) and do not know how to accomplish. I have recorded a macro during the initial chart creation in the "blank" worksheet but it returns a "compile error". Sample attached with three worksheets, data and macro. All help would be appreciated.
    Attached Files Attached Files

  2. #2
    Forum Guru mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2007/2010
    Posts
    2,815

    Re: Adding charts to multiple worksheets automatically

    I've modified your recorded code to create the first of your graphs on each sheet. Hopefully you will be able to use this example to adapt the remainder.

    Code:
    Sub Autochart()
    
    For Each Sheet In ThisWorkbook.Sheets
        If ActiveWindow.Visible = True Then
            Sheet.Activate
            Range("T3:V33").Select
            Charts.Add
            ActiveChart.ChartType = xlLineMarkers
            ActiveChart.Location Where:=xlLocationAsObject, Name:=Sheet.Name
            ActiveChart.SetSourceData Source:=ActiveSheet.Range("T3:V33"), PlotBy:= _
                xlColumns
            
            With ActiveChart
                .HasTitle = True
                .ChartTitle.Characters.Text = "SPC Data Dimension (Average)"
                .Axes(xlCategory, xlPrimary).HasTitle = False
                .Axes(xlValue, xlPrimary).HasTitle = False
            End With
            With Selection.Interior
                .ColorIndex = 35
                .Pattern = 1
            End With
            ActiveChart.ChartArea.Left = 1300
            ActiveChart.ChartArea.Top = 600
            ActiveChart.ChartArea.Width = 450
            ActiveChart.ChartArea.Height = 300
            ActiveChart.SeriesCollection(2).Select
            With Selection.Border
                .ColorIndex = 1
                .Weight = xlMedium
                .LineStyle = xlDash
            End With
            With Selection
                .MarkerBackgroundColorIndex = xlAutomatic
                .MarkerForegroundColorIndex = xlAutomatic
                .MarkerStyle = xlNone
                .Smooth = False
                .MarkerSize = 7
                .Shadow = False
            End With
            ActiveChart.Legend.Select
            Selection.Position = xlBottom
            ActiveChart.SeriesCollection(3).Select
            With Selection.Border
                .ColorIndex = 1
                .Weight = xlMedium
                .LineStyle = xlDash
            End With
            With Selection
                .MarkerBackgroundColorIndex = xlAutomatic
                .MarkerForegroundColorIndex = xlAutomatic
                .MarkerStyle = xlNone
                .Smooth = False
                .MarkerSize = 7
                .Shadow = False
            End With
        End If
    Next Sheet
    End Sub
    Martin

    Eighty Twenty Spreadsheet Automation http://homepage.ntlworld.com/martin.rice1/ for all your Excel customisation and consulting needs.

    If my solution has saved you time and/or money, please consider donating to Cancer Research UK.

  3. #3
    Registered User
    Join Date
    10-30-2009
    Location
    Wisconsin, USA
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Adding charts to multiple worksheets automatically

    A run time error 91 (Object variable or with block variable not set) occurs at line "With Selection.Interior". I tried to comment this out to see if it would run beyond this line, but it does not. Any thoughts or suggestions?

  4. #4
    Forum Guru mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2007/2010
    Posts
    2,815

    Re: Adding charts to multiple worksheets automatically

    Try commenting this and the following three lines as an experiment.
    Martin

    Eighty Twenty Spreadsheet Automation http://homepage.ntlworld.com/martin.rice1/ for all your Excel customisation and consulting needs.

    If my solution has saved you time and/or money, please consider donating to Cancer Research UK.

  5. #5
    Registered User
    Join Date
    10-30-2009
    Location
    Wisconsin, USA
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Adding charts to multiple worksheets automatically

    I commented out the four lines and then got a compile error on [ActiveChart.SetSourceData Source:=Sheets("Blank").Range("X3:Y33"), PlotBy:=] line. I'm afraid I don't know enough about the coding to understand too much of what is, or is not going on.
    Any recommendations on learning materials?

    Thank you for your efforts.
    P

  6. #6
    Forum Guru mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2007/2010
    Posts
    2,815

    Re: Adding charts to multiple worksheets automatically

    You've got an extra carriage return in there. The line needs to be all on one line.

    Code:
        ActiveChart.SetSourceData Source:=Sheets("Blank").Range("X3:Y33"), PlotBy:=xlColumns
    I would suggest that after making the above change, paste my code into a new module and change the name of the sub so that we don't have two with the same name (e.g. change to Autochart1).

    Code:
    Sub Autochart1()
    
    For Each Sheet In ThisWorkbook.Sheets
        If ActiveWindow.Visible = True Then
            Sheet.Activate
    
    'repeat this section with the appropriate change in ranges and parameters
            Range("T3:V33").Select  'Change here
            Charts.Add
            ActiveChart.ChartType = xlLineMarkers
            ActiveChart.Location Where:=xlLocationAsObject, Name:=Sheet.Name
            ActiveChart.SetSourceData Source:=ActiveSheet.Range("T3:V33"), PlotBy:= xlColumns  'Change here
    
            
            With ActiveChart
                .HasTitle = True
                .ChartTitle.Characters.Text = "SPC Data Dimension (Average)"  'Change here
    
                .Axes(xlCategory, xlPrimary).HasTitle = False
                .Axes(xlValue, xlPrimary).HasTitle = False
            End With
            With Selection.Interior
                .ColorIndex = 35
                .Pattern = 1
            End With
            ActiveChart.ChartArea.Left = 1300  'Change here
            ActiveChart.ChartArea.Top = 600 'Change here
            ActiveChart.ChartArea.Width = 450 'Change here
            ActiveChart.ChartArea.Height = 300 'Change here
            ActiveChart.SeriesCollection(2).Select
            With Selection.Border
                .ColorIndex = 1
                .Weight = xlMedium
                .LineStyle = xlDash
            End With
            With Selection
                .MarkerBackgroundColorIndex = xlAutomatic
                .MarkerForegroundColorIndex = xlAutomatic
                .MarkerStyle = xlNone
                .Smooth = False
                .MarkerSize = 7
                .Shadow = False
            End With
            ActiveChart.Legend.Select
            Selection.Position = xlBottom
            ActiveChart.SeriesCollection(3).Select
            With Selection.Border
                .ColorIndex = 1
                .Weight = xlMedium
                .LineStyle = xlDash
            End With
            With Selection
                .MarkerBackgroundColorIndex = xlAutomatic
                .MarkerForegroundColorIndex = xlAutomatic
                .MarkerStyle = xlNone
                .Smooth = False
                .MarkerSize = 7
                .Shadow = False
            End With
        End If
    'End of repeat section
    
    Next Sheet
    End Sub
    Martin

    Eighty Twenty Spreadsheet Automation http://homepage.ntlworld.com/martin.rice1/ for all your Excel customisation and consulting needs.

    If my solution has saved you time and/or money, please consider donating to Cancer Research UK.

Thread Information

Users Browsing this Thread

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

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.2.0