+ Reply to Thread
Results 1 to 9 of 9

Macro to Place chart in specified Range

Hybrid View

shiva_reshs Macro to Place chart in... 09-14-2012, 08:26 AM
Domenic Re: Macro to Place chart in... 09-14-2012, 06:17 PM
shiva_reshs Re: Macro to Place chart in... 09-17-2012, 03:06 AM
shiva_reshs Re: Macro to Place chart in... 09-17-2012, 03:33 AM
shiva_reshs Re: Macro to Place chart in... 09-17-2012, 05:01 AM
Andy Pope Re: Macro to Place chart in... 09-17-2012, 05:09 AM
shiva_reshs Re: Macro to Place chart in... 09-17-2012, 06:41 AM
Andy Pope Re: Macro to Place chart in... 09-17-2012, 06:49 AM
shiva_reshs Re: Macro to Place chart in... 09-17-2012, 07:08 AM
  1. #1
    Forum Contributor
    Join Date
    07-17-2012
    Location
    India
    MS-Off Ver
    Excel 2016
    Posts
    715

    Macro to Place chart in specified Range

    Hi All,

    Charts.Add
        ActiveChart.SetSourceData Source:=Sheets("OM Error Trend").Range("K7")
        ActiveChart.Location Where:=""
    Every time I execute the code it creates the chart but in new sheet. I want to place it in same sheet in L1 and below range.

    FYI, My first code "Sheets("OM Error Trend").Range("K7")" is located near pivot so it picks the graph from there.

    Help needed.

    Regards
    Shiva
    Last edited by shiva_reshs; 09-17-2012 at 07:09 AM. Reason: Solved

  2. #2
    Forum Expert
    Join Date
    06-18-2004
    Location
    Canada
    MS-Off Ver
    Office 2016
    Posts
    1,474

    Re: Macro to Place chart in specified Range

    Maybe something like this...

        Dim wksDest As Worksheet
        Dim MyChart As Chart
        
        Set wksDest = Worksheets("OM Error Trend")
        
        With wksDest
            Set MyChart = .ChartObjects.Add( _
                Left:=.Range("L1").Left, _
                Top:=.Range("L1").Top, _
                Width:=300, _
                Height:=200).Chart
        End With
        
        With MyChart
            .SetSourceData wksDest.PivotTables("PivotTable1").TableRange2
            .ChartType = xl3DColumnClustered
        End With
    Change the width, height, pivot table name, and chart type accordingly.

  3. #3
    Forum Contributor
    Join Date
    07-17-2012
    Location
    India
    MS-Off Ver
    Excel 2016
    Posts
    715

    Re: Macro to Place chart in specified Range

    Hi,

    Thanks a lot of your help. It work perfectly.

    Rep Added.

    Regards,
    Shiva

  4. #4
    Forum Contributor
    Join Date
    07-17-2012
    Location
    India
    MS-Off Ver
    Excel 2016
    Posts
    715

    Re: Macro to Place chart in specified Range

    need one small help here. Chart type which I am using is "Colored Lines" but it seems that option is not available in VBA to choose from.

    "Colored Lines" chart is available in Custom Charts.

    Note : It is a chart with Lines and black background.

    Any help on this.

    Regards
    Shiva
    Last edited by shiva_reshs; 09-17-2012 at 03:34 AM. Reason: error in typing

  5. #5
    Forum Contributor
    Join Date
    07-17-2012
    Location
    India
    MS-Off Ver
    Excel 2016
    Posts
    715

    Re: Macro to Place chart in specified Range

    Hi All,

    I was able to create the graph almost similar to the COlored Lines graph.

    Dim wksDest As Worksheet
        Dim MyChart As Chart
        
        Set wksDest = Worksheets("OM Error Trend")
        
        With wksDest
            Set MyChart = .ChartObjects.Add( _
                Left:=.Range("A54").Left, _
                Top:=.Range("A54").Top, _
                Width:=500, _
                Height:=300).Chart
        End With
        
        With MyChart
            .SetSourceData wksDest.PivotTables("PivotTable1").TableRange2
            .ChartType = xlLine
    But Can anyone tell me the way to select this graph and choose background colour as Black?

    Regards
    Shiva

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

    Re: Macro to Place chart in specified Range

    Macro recorder produced this code

        ActiveChart.ApplyCustomType ChartType:=xlBuiltIn, TypeName:="Colored Lines"
    Cheers
    Andy
    www.andypope.info

  7. #7
    Forum Contributor
    Join Date
    07-17-2012
    Location
    India
    MS-Off Ver
    Excel 2016
    Posts
    715

    Re: Macro to Place chart in specified Range

    I tried that adding below my code.

    Here is the error Message " Object Variable or with block variable not set.
    Error stoped at "ActiveChart.ApplyCustomType ChartType:=xlBuiltIn, TypeName:="Colored Lines" in VBA

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

    Re: Macro to Place chart in specified Range

    I would assume then that the chart was not active.

    Dim wksDest As Worksheet
        Dim MyChart As Chart
        
        Set wksDest = Worksheets("OM Error Trend")
        
        With wksDest
            Set MyChart = .ChartObjects.Add( _
                Left:=.Range("A54").Left, _
                Top:=.Range("A54").Top, _
                Width:=500, _
                Height:=300).Chart
        End With
        
        With MyChart
            .SetSourceData wksDest.PivotTables("PivotTable1").TableRange2
            .ChartType = xlLine
            .ApplyCustomType ChartType:=xlBuiltIn, TypeName:="Colored Lines" in VBA 
        End with

  9. #9
    Forum Contributor
    Join Date
    07-17-2012
    Location
    India
    MS-Off Ver
    Excel 2016
    Posts
    715

    Re: Macro to Place chart in specified Range

    Wow, I am not sure how you did it. But it is working flawlessly.

    Thanks a bunch.
    Rep added.

    Regards
    Shiva

+ Reply to Thread

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.6.0 RC 1