Create Chart in a new Workbook using VBA

ChaosPup

New Member
Joined
Sep 27, 2021
Messages
48
Office Version
  1. 365
Platform
  1. Windows
Hello all,

At the moment I run a macro to create a new chart in a new worksheet -

VBA Code:
Sub create_chart_sheet()
Dim oChartSheet As Chart

Set oChartSheet = Charts.Add

oChartSheet.HasTitle = True
oChartSheet.ChartTitle.Text = "CARs By Product"

oChartSheet.HasLegend = False

oChartSheet.SetSourceData Sheets("COUNTS").Range("A3:B51")

End Sub

This works fine, what I really want though is for this macro to create a new workbook and add the new chart to it without making any changes to the source workbook. Is this possible?

Thanks!
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Try the following code....

VBA Code:
Sub create_chart_sheet()

    'get the range for the source data
    Dim sourceDataRange As Range
    Set sourceDataRange = ThisWorkbook.Sheets("COUNTS").Range("A3:B51")
    
    'create a new workbook that contains a single blank worksheet
    Dim destinationWorkbook As Workbook
    Set destinationWorkbook = Workbooks.Add(xlWBATWorksheet)

    'create a new chart sheet and place it at the beginning of the destination workbook
    Dim oChartSheet As Chart
    Set oChartSheet = destinationWorkbook.Charts.Add(destinationWorkbook.Sheets(1))
    
    'set the properties for the chart
    With oChartSheet
        .HasTitle = True
        .ChartTitle.Text = "CARs By Product"
        .HasLegend = False
        .SetSourceData sourceDataRange
    End With
    
    'delete the unused worksheet in the destination workbook (optional)
    Application.DisplayAlerts = False
    destinationWorkbook.Sheets(2).Delete
    Application.DisplayAlerts = True

End Sub

Hope this helps!
 
Upvote 0
Solution

Forum statistics

Threads
1,214,591
Messages
6,120,428
Members
448,961
Latest member
nzskater

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