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

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
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,224,616
Messages
6,179,908
Members
452,949
Latest member
beartooth91

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