Paste Chart Image as new worksheet

gdesreu

Active Member
Joined
Jul 30, 2012
Messages
318
I was hoping someone has an answer for this.

I have a chart that I want to copy and paste as an image on a new worksheet. The excerpt (bottom) works fine except that I want to paste it as a new worksheet NOT on top of a new worksheet. You can do this when moving a chart as new worksheet using
VBA Code:
ActiveChart.Location Where:=xlLocationAsNewSheet
but I'm a bit confused as to how to copy the chart as an image and then insert it as a new worksheet.

Any help (as always) is greatly appreciated.

Working Code that pastes as image onto new worksheet.
VBA Code:
    Selection.Copy
    Sheets("example").Select
    Range("B1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        
        
    ActiveSheet.ChartObjects("Chart 3").Activate
    ActiveChart.ChartArea.Copy
    Sheets.Add After:=ActiveSheet
    ActiveSheet.Pictures.Paste.Select
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
I'm assuming that the reason you want to paste your chart as a picture is so that there's no link between the newly created chart sheet and the original chart. If so, try the following macro, which will copy the chart from the active sheet, create a new chart sheet, paste the chart into the newly created chart sheet, and then convert the formulas into values...

VBA Code:
Option Explicit

Sub test()

    ActiveSheet.ChartObjects("Chart 3").Chart.ChartArea.Copy
   
    DoEvents

    Dim chartSheet As Object
    Set chartSheet = ThisWorkbook.Charts.Add
   
    DoEvents
   
    With chartSheet
   
        Do While .SeriesCollection.Count > 0
            .SeriesCollection(1).Delete
        Loop
       
        .Paste
       
        DoEvents
   
        With .SeriesCollection(1)
            .XValues = .XValues
        End With
   
        Dim sr As Series
        For Each sr In .SeriesCollection
            sr.Name = "=""" & sr.Name & """"
            sr.Values = sr.Values
        Next sr
       
    End With
   
End Sub

Hope this helps!
 
Upvote 0
I'm assuming that the reason you want to paste your chart as a picture is so that there's no link between the newly created chart sheet and the original chart. If so, try the following macro, which will copy the chart from the active sheet, create a new chart sheet, paste the chart into the newly created chart sheet, and then convert the formulas into values...

VBA Code:
Option Explicit

Sub test()

    ActiveSheet.ChartObjects("Chart 3").Chart.ChartArea.Copy
  
    DoEvents

    Dim chartSheet As Object
    Set chartSheet = ThisWorkbook.Charts.Add
  
    DoEvents
  
    With chartSheet
  
        Do While .SeriesCollection.Count > 0
            .SeriesCollection(1).Delete
        Loop
      
        .Paste
      
        DoEvents
  
        With .SeriesCollection(1)
            .XValues = .XValues
        End With
  
        Dim sr As Series
        For Each sr In .SeriesCollection
            sr.Name = "=""" & sr.Name & """"
            sr.Values = sr.Values
        Next sr
      
    End With
  
End Sub

Hope this helps!
This is exactly what I was looking for! I cant thank you enough. Much appreciated!
 
Upvote 0
You're very welcome, I'm glad I could help.

Cheers!
Domenic, one more thing I just noticed, (sorry I didnt pick this up earlier) Is there a way to set the objects on the chart to a value as well like you did with the series? I have the chart title linked to a cell which loops through different samples so when I look at it now it still has the cell reference and not the value. Again, thanks, this is super helpful.
 
Upvote 0
Also, if I try to run the code twice, for example if I make a second histogram it deletes everything on the second chart but the first one seems ok. - Thanks
 
Upvote 0
I have amended my macro so that it converts the formula for the chart title into a value. Also, notice that have declared chartSheet as Chart instead of Object.

VBA Code:
Option Explicit

Sub test()

    ActiveSheet.ChartObjects("Chart 3").Chart.ChartArea.Copy
    
    DoEvents

    Dim chartSheet As Chart
    Set chartSheet = ThisWorkbook.Charts.Add
    
    DoEvents
    
    With chartSheet
    
        Do While .SeriesCollection.Count > 0
            .SeriesCollection(1).Delete
        Loop
        
        .Paste
        
        DoEvents
        
        If .HasTitle Then
            .ChartTitle.Caption = .ChartTitle.Caption
        End If
    
        With .SeriesCollection(1)
            .XValues = .XValues
        End With
    
        Dim sr As Series
        For Each sr In .SeriesCollection
            sr.Name = "=""" & sr.Name & """"
            sr.Values = sr.Values
        Next sr
        
    End With
    
End Sub
 
Upvote 0
Also, if I try to run the code twice, for example if I make a second histogram it deletes everything on the second chart but the first one seems ok. - Thanks

Can you please clarify?
 
Upvote 0
I think I know what you mean. It's probably a timing issue. I'll post an amended version of my macro shortly.
 
Upvote 0
Okay, I have amended the code so that it pauses at certain times to allow the operation time to complete. Currently, when it pauses, it does so for 1 second, which should be enough time. However, if you still run into problems, you can always try pausing it for more seconds, such as 2 or 3 seconds. Here's the amended code, which includes an additional procedure called Pause.

VBA Code:
Option Explicit

Sub test()

    ActiveSheet.ChartObjects("Chart 3").Chart.ChartArea.Copy
    
    Pause 1 'second

    Dim chartSheet As Chart
    Set chartSheet = ThisWorkbook.Charts.Add
    
    Pause 1 'second
    
    With chartSheet
    
        Do While .SeriesCollection.Count > 0
            .SeriesCollection(1).Delete
        Loop
        
        .Paste
        
        Pause 1 'second
        
        If .HasTitle Then
            .ChartTitle.Caption = .ChartTitle.Caption
        End If
    
        With .SeriesCollection(1)
            .XValues = .XValues
        End With
    
        Dim sr As Series
        For Each sr In .SeriesCollection
            sr.Name = "=""" & sr.Name & """"
            sr.Values = sr.Values
        Next sr
        
    End With
    
End Sub

Sub Pause(ByVal secs As Long)

    Dim endTime As Single
    endTime = Timer + secs
    
    Do
        DoEvents
    Loop Until Timer > endTime
    
End Sub

Does this help?
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top