56 graphs on one sheet

Daisyverma

New Member
Joined
Jul 11, 2019
Messages
1
Hey everyone
I have a spreadsheet with 56 graphs on one sheet. Is there a macro that will cut and paste each chart and place on a separate worksheet?
A bonus would be if the worksheet can reflect the chart title (department/unit name)
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Here's one way to do it.

Make sure you change the name of the sheet on the line indicated in the code.

It's the first macro that should be run.

Code:
Public Sub CopyCharts()
  Const strSHEET_WITH_CHARTS = "Sheet1"   '<--- Set name of sheet containing charts
  Dim intCounter As Integer
  Dim wksTarget As Worksheet
  Dim chtObj As ChartObject
  On Error GoTo ErrorHandler
  For Each chtObj In ThisWorkbook.Sheets(strSHEET_WITH_CHARTS).ChartObjects
    Set wksTarget = CreateWorksheet(chtObj.Chart.ChartTitle.Text)
    chtObj.Copy
    wksTarget.Paste
    intCounter = intCounter + 1
  Next chtObj
  ThisWorkbook.Sheets(strSHEET_WITH_CHARTS).Activate
  MsgBox intCounter & " chart(s) were copied.", vbInformation
ExitHandler:
  Set wksTarget = Nothing
  Set chtObj = Nothing
  Exit Sub
ErrorHandler:
  MsgBox Err.Description, vbExclamation
  Resume ExitHandler
End Sub

Private Function CreateWorksheet(ByVal strSheetName As String) As Worksheet
  Dim strNewName As String
  Dim intCounter As Integer
  strNewName = strSheetName
  Do While SheetExists(strNewName)
    intCounter = intCounter + 1
    strNewName = strSheetName & " (" & intCounter & ")"
  Loop
  With ThisWorkbook.Sheets
    Set CreateWorksheet = .Add(, .Item(.Count))
    CreateWorksheet.Name = strNewName
  End With
End Function

Private Function SheetExists(ByVal strSheetName As String) As Boolean
  Dim objSheet As Object
  On Error Resume Next
  Set objSheet = ThisWorkbook.Sheets(strSheetName)
  On Error GoTo 0
  SheetExists = Not (objSheet Is Nothing)
  Set objSheet = Nothing
End Function
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
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